Win32 QT and Lua interaction using (iii): Connect QT object in Lua script

Source: Internet
Author: User
Tags lua

Answer the above. In order to facilitate the use of LUA, I have written a Lua class. The main code is as follows:

QLua.h

1 #ifndef qlua_h 2 #define QLUA_H 3  4//own 5 #include "include/lua.hpp" 6  7//qt 8 #include <QObject> 9 # Include <qfile>10 #include <qdebug>11 #include <qwidget>13 #include <qlineedit>14 #include & Lt qpushbutton>15 #include <qmessagebox>16 class qlua:public QObject18 {q_object20 public:21 QLua     ( Qobject *parent = 0);     ~qlua (); n     /lua25     void init (), +     void Close (),     void Pushfunction ( QString FuncName, lua_cfunction func),     Beginmodule (QString),     void AddType (QString, lua_cfunction Deletefunc),     moduletypefunction (QString, lua_cfunction), void     endmodule (), void     run (  QString) signals:37 46 public slots:39 private:41 lua_state *luastate;42 approx     bool isstartmodule;44};45 #endif//Qlua_h

QLua.cpp

 1 #include "qlua.h" 2 3 Qlua::qlua (Qobject *parent): 4 qobject (parent) 5, Luastate (NULL) 6, Isstartmodule (FAL SE) 7 {8 init (), 9}10 One Qlua::~qlua () () (),}15 (qlua::init), luastate = Lual_newst Ate (); Lual_openlibs (luastate),}21 void Qlua::close (), (luastate! = NULL) + Lua_clo SE (luastate); luastate = null;28}29}30 to void QLua::p ushfunction (QString funcName, lua_cfunction func) 32 {funcname.isempty ()) return;34 if (func = = NULL) return;35 lua_pushcfunction (Luastate, func); 3 7 Lua_setglobal (Luastate, Funcname.tolocal8bit (). Data ()), Max}39, Qlua::beginmodule (QString name), if (l Uastate = = NULL) return;43 if (Isstartmodule = = False) {Tolua_open (luastate); Tolua_modu Le (luastate, NULL, 0), Isstartmodule = true;49}50, const char *STR = Name.isempty ()?     NULL:name.toLocal8Bit (). data (); 52Tolua_beginmodule (Luastate, str),}54 qlua::addtype (QString name, Lua_cfunction deletefunc) Te = = null) return;58 if (Name.isempty ()) return;59 if (Deletefunc = = null) return;60 Tolua_usertype (LuaS Tate, Name.tolocal8bit (). data ()); const char *STR = Name.tolocal8bit (). data (); Tolua_cclass (luastate, str, str      , "", Deletefunc); Beginmodule (name),}66 (QString name, lua_cfunction func) 68 {69 if (luastate = = NULL) return, if (Name.isempty ()) return;71 const char *STR = Name.tolocal8bit (). Data ()  ; Tolua_function (Luastate, str, func);}75 tolua_endmodule void Qlua::endmodule () (luastate); 79}80 81 void Qlua::run (QString str) lual_loadbuffer (luastate, Str.tolocal8bit (). Data (), Str.length (), "line"); A_pcall (luastate, 0, 0, 0); 85}

The Qlua class makes it easy to implement some simple LUA operations, such as initializing, shutting down, running LUA code, binding functions, and so on.

What I want to do now is to be able to generate Qt objects in Lua code, and then connect the signals and slots of the QT native objects. So how does it work?

The function for connecting signals and slots in Qt is Qobject::connect (Qobject * A, SIGNAL (), Qobject * b, slot ()). First, we need to figure out what the signal and slots really are.

From the parameters list of connect, we can clearly see that the result of signal and slot is a string of type char*. We can right-click on the signal and go to the definition of signal. Or do a simple experiment. Test the following QT code:

    Qdebug () << qstring::fromlocal8bit (SIGNAL (clicked ()));    Qdebug () << qstring::fromlocal8bit (SLOT (Close ()));

The results are interesting:

The simple summary is that we simply pass in the string of the function name, plus the prefix. The prefix of signal is the prefix of 2,slot is 1.

This allows us to implement the Connect function in Lua. It is still written in C + + and then bound to Lua. As follows:

1 static int Connect (lua_state* state) 2 {3     qobject * a = (qobject*) tolua_tousertype (state, 1, 0); 4     const char * Signal = tolua_tostring (state, 2, 0); 5     Qobject * b = (qobject*) tolua_tousertype (state, 3, 0); 6     const char * slot = Tolua_tostring (state, 4, 0); 7  8     Qobject::connect (A, QString ("2%0"). Arg (signal). Tolocal8bit (). Data (), 9                      B, QString ("1%0"). Arg (slot). Tolocal8bit (). data ()); 10}

Binding can use the Qlua class above:

Lua.pushfunction ("Connect", connect);

The complete main.cpp code is as follows:

#include "include/lua.hpp" #include "qlua.h" #include <QWidget> #include <QApplication> #include <qfile  > #include <qdebug>static int Test (lua_state* state) {qpushbutton* a = (qpushbutton*) tolua_tousertype (state, 1,    0); if (a) a->show ();}    static int Connect (lua_state* state) {Qobject * a = (qobject*) tolua_tousertype (state, 1, 0);    const char * signal = tolua_tostring (state, 2, 0);    Qobject * b = (qobject*) tolua_tousertype (state, 3, 0);    const char * slot = Tolua_tostring (state, 4, 0); Qobject::connect (A, QString ("2%0"). Arg (signal). Tolocal8bit (). data (), B, QString ("1%0"). Arg (slot). Toloc Al8bit (). data ());}    static int Tolua_new_qwidget (lua_state* state) {qwidget* widget = new Qwidget ();    Tolua_pushusertype (state, Widget, "Qwidget"); return 1;}    static int Tolua_delete_qwidget (lua_state* state) {qdebug () << "Delete Start";    qwidget* widget = (qwidget*) tolua_tousertype (state, 1, 0);   if (NULL! = widget) {     Qdebug () << "delete~";        Widget->close ();    Delete widget; } return 1;}    static int Tolua_show_qwidget (lua_state* state) {qwidget* widget = (qwidget*) tolua_tousertype (state, 1, 0);    if (widget = NULL) {widget->show (); } return 1;}    static int Tolua_new_qpushbutton (lua_state* state) {qpushbutton* button = new Qpushbutton ();    Tolua_pushusertype (State, Button, "Qpushbutton"); return 1;} static int Tolua_delete_qpushbutton (lua_state* state) {qpushbutton* button = (qpushbutton*) tolua_tousertype (state, 1,    0);        if (NULL! = button) {button->close ();    Delete button; } return 1;} static int Tolua_show_qpushbutton (lua_state* state) {qpushbutton* button = (qpushbutton*) tolua_tousertype (state, 1, 0)    ;    if (Button! = NULL) {button->show (); } return 1;}  static int Tolua_settext_qpushbutton (lua_state* state) {qpushbutton* button = (qpushbutton*) tolua_tousertype (state, 1,    0); Const CHAR * Text = tolua_tostring (state, 2, 0);    if (Button! = NULL) {button->settext (qstring::fromlocal8bit (text)); } return 1;}    static int Tolua_resize_qwidget (lua_state* state) {qwidget* widget = (qwidget*) tolua_tousertype (state, 1, 0);    Double A = Tolua_tonumber (state, 2, 0);    Double b = Tolua_tonumber (state, 3, 0);    if (widget) {widget->resize ((int) A, (int) b); } return 1;} static int qapplication_instance (lua_state* state) {Tolua_pushusertype (state, Qapplication::instance (), "    Qapplication "); return 1;}    static int qapplication_quit (lua_state* state) {qapplication * app = (qapplication *) Tolua_tousertype (state, 1, 0);    if (APP) App->quit (); return 1;} static int Qapplication_delete (lua_state*) {return 1;}    int main (int argc, char * argv[]) {q_init_resource (resources);    Qapplication A (argc, argv);    QLua Lua;    Lua.beginmodule ("");    Lua.addtype ("Qwidget", tolua_delete_qwidget); Lua.moduletypefunction ("New", TolUa_new_qwidget);    Lua.moduletypefunction ("show", Tolua_show_qwidget);    Lua.moduletypefunction ("resize", tolua_resize_qwidget);    Lua.endmodule ();    Lua.addtype ("Qpushbutton", Tolua_delete_qpushbutton);    Lua.moduletypefunction ("new", Tolua_new_qpushbutton);    Lua.moduletypefunction ("show", Tolua_show_qpushbutton);    Lua.moduletypefunction ("SetText", Tolua_settext_qpushbutton);    Lua.endmodule ();    Lua.addtype ("Qapplication", qapplication_delete);    Lua.moduletypefunction ("instance", Qapplication_instance);    Lua.moduletypefunction ("Quit", qapplication_quit);    Lua.endmodule ();    Lua.endmodule ();    Lua.pushfunction ("Test", test);    Lua.pushfunction ("Connect", connect);    Read resource files QFile file ("://test.lua"); File.Open (Qiodevice::readonly |    Qiodevice::text);    Qtextstream in (&file);    In.setcodec ("UTF-8");    Executive Lua.run (In.readall ()); return a.exec ();}

The Test.lua code is as follows:

Widget = Qwidget:new () widget:show () button = Qpushbutton:new () button:settext ("China") Button:show () Connect (button, " Clicked () ", Widget," Close () ") Connect (button," clicked () ", Button," Close () ") print (" Run Over ")

The results of the program run as follows:

Click the China button and the button and Widget window will be closed.

At this point, the signals and slots for connecting Qt objects have been realized.

If you continue, you can fully implement classes and objects that use QT in Lua scripts and write out Lua GUI programs.

Complete code project file: HTTP://PAN.BAIDU.COM/S/1NTMFDJJ

I have a blog address: http://www.cnblogs.com/IT-BOY/

Win32 QT and Lua interaction using (iii): Connect QT object in Lua script

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.