QLua
Lua-Qt bindings
QLua

QLua is a Qt <--> Lua binding framework.

QObjects are exposed to Lua as tables with values matching QObject properties and callable methods declared as signals, slots or Q_INVOKABLE.

The public interface is defined in the qlua::LuaContext class.

Usage

QLua is used to access exported QObject methods and properties from Lua and to connect QObject signals to Lua functions or QObject slots.

Initialization

Create or wrap a lua_State with qlua::LuaContext constructor. Add QObjects with qlua::LuaContext::AddQObject method.

#include <LuaContext.h>
...
using namespace qlua;
...
LuaContext lc;  // create a new lua_State
LuaContext lcw( luaStatePtr ) ; // wraps an existing lua_State
...
MyQObject myobj;
lc.AddQObject( &myobj, "myobj" ); // add MyQObject instance to lua_State
...

Method invocation

Each QObject is added to Lua as a table, with the name specified in the qlua::LuaContext::AddQObject method invocation. You invoke a method from Lua as a function stored inside a table.

myobj.aMethod('anArgument');

Signals

Signals can be connected to

  1. Lua functions
  2. QObject methods

In the case of (1) a function is passed as the endpoint of a signal connection. (2) requires both a target QObject and a method signature.

QObjects are passed to the qlua.connect function as QObject instances previously added through qlua::LuaContext::AddQObject or as pointers added to Lua by calling lua_pushlightuserdata.

qlua.connect( myobj, 'aSignal(QString)',
              function( msg ) print( msg ) end );
myobj.emitSignal( 'hello' ); 

Memory management

QObjects added to Lua with qlua::LuaContext::AddQObject are not garbage collected by default.

To have a QObject deleted when the Lua table is garbage collected set the value of the deleteMode parameter in the invocation of qlua::LuaContext::AddQObject to one of:

See also:
qlua::LuaContext::ObjectDeleteMode

The deleteMode parameter will be replaced in the future with a custom deleter.

 All Classes Namespaces Files Functions Variables Enumerations Enumerator