1 #include "common_bind.hpp"
2 #include "luafunction.hpp"
3 #include "lqt_bind_QLabel.hpp"
4 #include "lqt_bind_QCoreApplication.hpp"
5 #include "lqt_bind_QApplication.hpp"
6 #include "lqt_bind_QAbstractButton.hpp"
7 #include "lqt_bind_QPushButton.hpp"
8 #include "lqt_bind_QObject.hpp"
9 #include "lqt_bind_QFont.hpp"
10 #include "lqt_bind_QWidget.hpp"
11 #include "lqt_bind_QTextEdit.hpp"
12 #include "lqt_bind_QAbstractScrollArea.hpp"
13 #include "lqt_bind_QFrame.hpp"
14 #include "lqt_bind_QEvent.hpp"
15 #include "lqt_bind_QLayout.hpp"
16 #include "lqt_bind_QBoxLayout.hpp"
17 #include "lqt_bind_QVBoxLayout.hpp"
19 #define EXPORT(n) do { \
20 luaL_getmetatable(L,#n "*" );\
21 lua_setglobal(L, #n);\
25 #include <QApplication>
26 #include <QPushButton>
31 #include <QVBoxLayout>
32 #include <QMessageBox>
37 static int retriever (lua_State
*L
) {
38 lua_pushvalue(L
, lua_upvalueindex(1));
42 static int F2S (lua_State
*L
) {
44 if (!lua_isfunction(L
, -1)) {
47 LuaFunction
*lf
= new LuaFunction(L
);
48 lua_pushstring(L
, "1function()");
52 int main(int argc
, char *argv
[])
54 QApplication
*app
= new QApplication(argc
, argv
);
58 lua_State
*L
= lua_open();
65 luaopen_QPushButton(L
);
66 luaopen_QAbstractButton(L
);
67 luaopen_QApplication(L
);
68 luaopen_QCoreApplication(L
);
70 luaopen_QAbstractScrollArea(L
);
74 luaopen_QBoxLayout(L
);
75 luaopen_QVBoxLayout(L
);
77 lua_pushcfunction(L
, F2S
);
78 lua_setglobal(L
, "SLOT");
86 EXPORT(QAbstractButton
);
92 lua_pushvalue(L
, LUA_REGISTRYINDEX
);
93 lua_setglobal(L
, "_R");
96 local del = QEvent.delete\
99 function search (k, v)\
100 if v == del then print ('here', v) return true end\
101 if type(v) == 'table' then\
104 for i, e in pairs(v) do\
105 if search(i, e) then print (k) end\
114 lqtL_pushudata(L
, app
, "QApplication*");
115 lua_pushcclosure(L
, retriever
, 1);
116 lua_setglobal(L
, "app");
118 lua_getglobal(L
, "QApplication");
119 lua_getglobal(L
, "app");
120 lua_setfield(L
, -2, "new");
123 lua_setglobal(L
, "app");
126 luaL_dofile(L
, "tutorial.lua");
127 qDebug() << lua_tostring(L
, -2) << lua_tostring(L
, -1);
129 lua_getglobal(L
, "PB");
132 lua_checkstack(L
, 15);
134 QWidget
*mainwin
= new LuaBinder
<QWidget
>(L
, 0, 0);
136 lqtL_pushudata(L
, app
, "QApplication*");
137 lua_setglobal(L
, "app");
140 lqtL_pushudata(L
, &butt
, "QPushButton*");
141 lua_setglobal(L
, "my_button");
142 luaL_dostring(L
, "my_button:show();my_button:connect('2pressed()', my_button, '1close()')");
143 luaL_dostring(L
, "my_button:setWindowTitle'lqt ROCKS!'");
144 luaL_dostring(L
, "my_button:setText'Hello World'");
145 luaL_dostring(L
, "my_button:setFixedSize(200, 100)");
146 qDebug() << lua_tostring(L
, -1);
149 //QWidget *mainwin = new QWidget(0, 0);
151 luaL_dostring(L
, "function override(t, k) local old_t = t[k] if type(old_t)~='function' then return end t[k] = function(...) print('overridden ', k, 'on', ...) return old_t(...) end end");
153 lqtL_pushudata(L
, mainwin
, "QWidget*");
154 lua_pushvalue(L
, -1);
155 lua_setglobal(L
, "mainwin");
156 luaL_dostring(L
, "my_button:show();my_button:connect('2pressed()', mainwin, '1close()')");
158 oldtop
= lua_gettop(L
);
159 //luaL_dostring(L, "print(mainwin.isWidgetType)");
160 luaL_dostring(L
, "print(mainwin:isWidgetType())");
161 luaL_dostring(L
, "mainwin:setWindowTitle'TestTitle'");
162 qDebug() << lua_tostring(L
, -1);
163 lua_settop(L
, oldtop
);
165 luaL_dostring(L
, "override(mainwin, '__gc')");
166 //luaL_dostring(L, "override(mainwin, '__index')");
167 //luaL_dostring(L, "override(mainwin, '__newindex')");
168 //luaL_dostring(L, "mainwin.qt_metacast = nil");
169 //luaL_dostring(L, "print(mainwin.qt_metacast)");
171 luaL_dostring(L
, "mainwin['~QWidget'] = function(obj) print('deleting', obj) end;");
172 //luaL_dostring(L, "local qtmc = mainwin.qt_metacast;"
173 //"mainwin.qt_metacast = function(...) print('overridden qt_metacast', ...) return qtmc(...) end;");
174 //luaL_dostring(L, "local hfw = mainwin.heightForWidth;"
175 //"mainwin.heightForWidth = function(...) print('overridden heightForWidth', ...) return hfw(...) end;");
176 //luaL_dostring(L, "print(mainwin.RenderFlag.IgnoreMask)");
178 QVBoxLayout
*qvbl
= new QVBoxLayout
;
179 QTextEdit
*qte
= new QTextEdit
;
180 QSpinBox
*qsb
= new QSpinBox
;
181 qvbl
->addWidget(qte
);
182 qvbl
->addWidget(qsb
);
184 mainwin
->setLayout(qvbl
);
186 qDebug() << "==============";
188 lua_getfield(L
, -1, "show");
189 lua_pushvalue(L
, -2);
192 lua_getfield(L
, -1, "width");
193 lua_pushvalue(L
, -2);
195 qDebug() << lua_tonumber(L
, -1) << mainwin
->width();
198 lua_getfield(L
, -1, "height");
199 lua_pushvalue(L
, -2);
201 qDebug() << lua_tonumber(L
, -1) << mainwin
->height();
204 lua_getfield(L
, -1, "isTopLevel");
205 lua_pushvalue(L
, -2);
207 qDebug() << (bool)lua_toboolean(L
, -1) << mainwin
->isTopLevel();
210 qDebug() << lua_gettop(L
) << lua_tostring(L
, 2);
212 lua_getfield(L
, -1, "heightForWidth");
213 lua_pushvalue(L
, -2);
214 lua_pushinteger(L
, 278);
216 qDebug() << lua_tonumber(L
, -1) << mainwin
->heightForWidth(278);
219 lua_getfield(L
, -1, "new");
221 lua_getfield(L
, -1, "show");
222 lua_pushvalue(L
, -2);
224 lqtL_manageudata(L
, -1);
227 //luaL_dostring(L, "child = mainwin.new();child:delete();");
228 lua_gc(L
, LUA_GCCOLLECT
, 0);
229 qDebug() << "=================";
230 luaL_dostring(L
, "w = mainwin.new();w:show();w:move(400, 400)");
231 lua_gc(L
, LUA_GCCOLLECT
, 0);
232 qDebug() << "=================";
234 lua_getfield(L
, -1, "move");
235 lua_pushvalue(L
, -2);
236 lua_pushinteger(L
, 100);
237 lua_pushinteger(L
, 100);
245 luaL_dostring(L
, "app:exec()");
247 qDebug() << "I will now close Lua state";