4 The qtlua interpreter tool

4.1 Description  

The QtLua project comes with a simple tool to experiment Qt enabled lua scripts.

Lua script files passed on command line are executed in left to right order. Standard lua libraries are loaded along with the QtLua lua libraries. the lua global variable app is wrapping the QApplication object.

This tool has an interactive mode which displays a Console window. This is useful for script debugging and allows playing with Qt objects from lua.

The interactive mode is automatically started if no script are passed on command line, it can also be invoked with the -i option on the command line and will start after scripts execution.

When in interactive mode, the list() and help() commands are useful to get started.

4.2 Example 1 : Hello world  

This simple example contains two files: a lua script file and an user interface file created with the Qt designer tool:

$ cd examples/lua/hello/
$ ls
hello.lua hello.ui

Lua script file content:

// code from examples/lua/hello/hello.lua:1

-- Load user interface file

ui = qt.load_ui("hello.ui");

-- Connect the quit button click signal to the application closeAllWindows slot.

qt.connect(ui.pushButton_2, "clicked", app, "closeAllWindows");

-- Connect the hello button click signal to a lua function which set the text content of the lineEdit widget.

qt.connect(ui.pushButton, "clicked",
function ()
ui.lineEdit:setText("hello world");
end
);

-- Show the user interface window

ui:show();

This window is displayed when the script is executed with the qtlua interpreter tool:

4.3 Example 2 : Console use  

This example shows how the qtlua interactive mode can be used to create widgets, set properties and play with Qt objects.

The first screenshot partially shows the list of available fields in a newly created QSpinBox widget:

The second screenshot shows how to update the spinbox buttons style property using the right Qt enumeration value:

Valid XHTML 1.0 StrictGenerated by diaxen on Mon Aug 15 03:23:06 2011 using MkDoc