4.1 The qtlua interpreter


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.

Example 1 : Hello world  

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

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

The hello_ui.lua has been generated from the hello.ui file by using the qtlua_uic tool.

The lua script file connect some signals and show the dialog:

#!/usr/bin/env qtlua

-- Load user interface file

ui = require("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");

-- Show the user interface window


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

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 Sat Mar 30 15:29:54 2013 using MkDoc