2 * Copyright (c) 2007 Mauro Iazzi
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
29 #include <QApplication>
30 #include <QPushButton>
35 #include <QVBoxLayout>
36 #include <QMessageBox>
39 #include "luadummy.hpp"
41 static int my_slot (lua_State
*L
) {
42 qDebug() << "my_slot called with" << lua_gettop(L
) << "arguments";
43 if ( lua_gettop(L
)==0 ) {
44 lua_getglobal(L
, "text_edit");
45 lua_getfield(L
, 1, "plainText");
47 luaL_dostring(L
, lua_tostring(L
, 1));
51 for (int i
=1;i
<=lua_gettop(L
);i
++) {
52 QVariant val
= lua_toqvariant(L
, i
);
53 mess
.append(QString::number(i
));
55 mess
.append(val
.toString());
58 QMessageBox::information(0, "Results", mess
);
62 static int lua_pushshortcut(lua_State
*L
) {
63 QWidget
*parent
= qobject_cast
<QWidget
*>(luaL_checkqobject(L
, 1));
64 QShortcut
*sc
= new QShortcut(parent
);
65 lua_pushqobject(L
, sc
);
69 static int lua_slot(lua_State
*L
) {
71 QObject
*dummy
= new LuaDummy(L
);
72 lua_pushqobject(L
, dummy
);
77 int main(int argc
, char *argv
[])
79 QApplication
app(argc
, argv
);
81 lua_State
*L
= lua_open();
85 lua_pushcfunction(L
, lua_pushshortcut
);
86 lua_setfield(L
, -2, "new");
87 lua_setglobal(L
, "QShortcut");
89 lua_pushcfunction(L
, lua_slot
);
90 lua_setglobal(L
, "SLOT");
93 lua_pushcfunction(L
, lqt_connect
);
94 lua_setfield(L
, -2, "connect");
95 lua_setglobal(L
, "qt");
97 luaL_checkstack(L
, 15, "shit");
99 // QSlider *hello2 = new QSlider;
100 // QSpinBox *hello = new QSpinBox;
101 // hello->resize(100, 30);
103 // hello2->resize(100, 100);
106 // lua_pushqobject(L, hello);
107 // lua_pushqobject(L, hello);
109 // lua_getfield(L, -1, "setValue");
110 // lua_pushqobject(L, hello);
111 // lua_pushinteger(L, 99);
112 // lua_call(L, 2, 0);
114 // lua_pushqobject(L, hello);
115 // lua_pushstring(L, "suffix");
116 // lua_pushstring(L, "suffix");
117 // lua_settable(L, -3);
118 // lua_pushstring(L, "prefix");
119 // lua_pushstring(L, "prefix");
120 // lua_settable(L, -3);
121 // lua_getfield(L, -1, "cleanText");
122 // qDebug() << lua_tostring(L, -1);
124 // lua_pushcfunction(L, lqt_connect);
125 // lua_pushqobject(L, hello2);
126 // lua_pushstring(L, "valueChanged(int)");
127 // lua_pushqobject(L, hello);
128 // lua_pushstring(L, "setValue ( int ) ");
129 // lua_call(L, 4, 1);
130 // qDebug() << (bool)lua_toboolean(L, -1);
132 // lua_pushcfunction(L, lqt_disconnect);
133 // lua_pushqobject(L, hello);
134 // lua_pushstring(L, "valueChanged(int)");
135 // lua_pushqobject(L, hello2);
136 // lua_pushstring(L, "setValue ( int ) ");
137 // lua_call(L, 4, 1);
138 // qDebug() << (bool)lua_toboolean(L, -1);
143 /* lua_pushcfunction(L, lqt_connect);
144 lua_pushqobject(L, &hello);
145 lua_pushstring(L, "destroyed()");
146 lua_pushqobject(L, &hello2);
147 lua_pushstring(L, "deleteLater()");
149 qDebug() << (bool)lua_toboolean(L, -1);
151 lua_pushcfunction(L, lqt_connect);
152 lua_pushqobject(L, &hello2);
153 lua_pushstring(L, "destroyed()");
154 lua_pushqobject(L, &hello);
155 lua_pushstring(L, "deleteLater()");
157 qDebug() << (bool)lua_toboolean(L, -1);
159 QObject::connect(&hello, SIGNAL(destroyed()), &hello2, SLOT(deleteLater()));*/
162 /* lua_pushqobject(L, b);
168 // lua_pushqobject(L, &hello);
169 // QVariant var = lua_toqvariant(L, -1);
171 // qDebug() << var.typeName();
174 QPushButton
*a
= new QPushButton("Exec");
175 // lua_pushqobject(L, a);
176 // lua_getfield(L, -1, "close");
177 // lua_pushqobject(L, a);
178 // lua_call(L, 1, 1);
179 /* qDebug() << lua_toqvariant(L, -1);*/
181 // lua_getfield(L, -1, "text");
182 // qDebug() << lua_toqvariant(L, -1);
188 lua_pushqobject(L
, &mainwin
);
189 lua_setglobal(L
, "mainwin");
191 QVBoxLayout
*qvbl
= new QVBoxLayout
;
192 QTextEdit
*qte
= new QTextEdit
;
193 QSpinBox
*qsb
= new QSpinBox
;
194 qvbl
->addWidget(qte
);
195 // qvbl->addWidget(hello);
196 // qvbl->addWidget(hello2);
198 qvbl
->addWidget(qsb
);
200 mainwin
.setLayout(qvbl
);
203 // lua_pushcfunction(L, methods_of);
204 lua_pushqobject(L
, qte
);
205 lua_setglobal(L
, "text_edit");
206 // lua_call(L, 1, 0);
209 lua_pushcfunction(L
, my_slot
);
210 int t
= lua_gettop(L
);
213 // lua_pushcfunction(L, methods_of);
215 QObject
* dummy2
= new LuaDummy(L
);
216 // lua_call(L, 1, 0);
220 lua_pushcfunction(L
, lqt_connect
);
221 lua_pushqobject(L
, a
);
222 lua_pushstring(L
, "pressed ()");
225 QObject
* dummy1
= new LuaDummy(L
);
226 lua_pushstring(L
, "generic_slot");
228 qDebug() << (bool)lua_toboolean(L
, -1);
232 lua_pushcfunction(L
, lqt_connect
);
233 lua_pushqobject(L
, qsb
);
234 lua_pushstring(L
, "valueChanged");
237 QObject
* dummy3
= new LuaDummy(L
);
238 lua_pushstring(L
, "generic_slot(int)");
240 qDebug() << (bool)lua_toboolean(L
, -1);
243 QShortcut
*sc
= new QShortcut(&mainwin
);
244 lua_pushcfunction(L
, lqt_connect
);
245 lua_pushqobject(L
, sc
);
246 lua_pushstring(L
, "Ctrl+P");
247 lua_setfield(L
, -2, "key");
248 lua_pushstring(L
, "activated()");
250 QObject
* dummy4
= new LuaDummy(L
);
251 lua_pushstring(L
, "generic_slot");
253 qDebug() << (bool)lua_toboolean(L
, -1);
256 luaL_dostring(L
, "return function (...) print(...) return true end");
257 QObject
* dummy5
= new LuaDummy(L
);
258 qsb
->installEventFilter(dummy5
);
263 // QMetaObject::invokeMethod(&hello, "setDisabled", Q_ARG(bool, true));
264 // QGenericArgument a(Q_ARG(bool, true));
265 // qDebug() << a.data() << a.name();
272 // QObject *pippo = new QObject::QObject;