Win32 QT and Lua in use (ii): Using QT classes in LUA scripts

Source: Internet
Author: User

The words are connected to the article. After successfully configuring Qt+lua+tolua, we can implement classes that use each QT in a Lua script. Look directly at the code.

#include "include/lua.hpp" #include <QWidget> #include <QApplication> #include <QFile> #include <    qdebug>static int Tolua_new_qwidget (lua_state* pState) {qwidget* widget = new Qwidget ();    Tolua_pushusertype (pState, widget, "Qwidget"); return 1;}    static int Tolua_delete_qwidget (lua_state* pState) {qwidget* widget = (qwidget*) tolua_tousertype (pState, 1, 0);        if (NULL! = widget) {widget->close ();    Delete widget; } return 1;}    static int Tolua_show_qwidget (lua_state* pState) {qwidget* widget = (qwidget*) tolua_tousertype (pState, 1, 0);    if (widget = NULL) {widget->show (); } return 1;}    static int Tolua_resize_qwidget (lua_state* pState) {qwidget* widget = (qwidget*) tolua_tousertype (pState, 1, 0);    Double A = Tolua_tonumber (pState, 2, 0);    Double b = Tolua_tonumber (pState, 3, 0);    Qdebug () << A;    Qdebug () << B;    if (widget) {widget->resize ((int) A, (int) b); } return 1;} InchT Main (int argc, char * argv[]) {q_init_resource (resources);    Qapplication A (argc, argv);    Initializes the Lua object lua_state *lu = Lual_newstate ();    Lual_openlibs (LU);    Use Tolua to "describe" the Qwidget class Tolua_open (LU);    Tolua_module (Lu, NULL, 0);    Tolua_beginmodule (Lu, NULL);    Tolua_usertype (Lu, "qwidget");    Tolua_cclass (Lu, "Qwidget", "Qwidget", "", tolua_delete_qwidget);    Tolua_beginmodule (Lu, "qwidget");    Tolua_function (Lu, "new", tolua_new_qwidget);    Tolua_function (Lu, "show", Tolua_show_qwidget);    Tolua_function (Lu, "resize", tolua_resize_qwidget);    Tolua_endmodule (LU);    Tolua_endmodule (LU);    Read resource files QFile file ("://test.lua"); File.Open (Qiodevice::readonly |    Qiodevice::text);    Note that the author uses the thing UTF-8 encoded, if it is ANSI code can be removed this sentence qtextstream in (&file);    In.setcodec ("UTF-8");    Read and print down to see if the correct QString str = In.readall ();    Qdebug () << str;    Lua loads and executes script Lual_loadbuffer (Lu, Str.tolocal8bit (). Data (), Str.length (), "line"); Lua_pcall (Lu, 0, 0, 0); return a.exec ();}

The author writes several functions of qwidget into the form of static and binds them with Tolua. This makes it possible to use a bound function smoothly in a LUA script. The contents of the following LUA script:

W = qwidget:new () w:show () w:resize (300,400)

If executed successfully, the result should be this:

You can see that a new Qwidget object has been successfully established and the size has changed.

This is one way to invoke the Qt class in Lua. We can bind most of the classes in the Qt class and use QT in our own LUA script.

The various operations on LUA can be encapsulated into classes, making the code more structured.

I think it's possible to make a lot of your ideas now, right?

With the complete project file, you may want to modify the relative path of the. lib file in the. Pro file:

Http://pan.baidu.com/s/1c0rbirm

With "LUA program Design" ebook, I hope that interested students can learn to communicate together:

Http://pan.baidu.com/s/1c01k2J2

Win32 QT and Lua in use (ii): Using QT classes in LUA scripts

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.