MetaType class reference
[Base module]

Declaration  

#include <QtLua/Value>

namespace QtLua {
template <typename X> class MetaType;
};

This class is a member of the QtLua namespace.

This abstract template class contains pure virtuals.

Description  

This class can be used to register conversion functions used by QtLua to convert between Lua values and C++ types registered as Qt meta types. It enables use of user defined types for QObject properties and slot parameters.

Consider the following user defined struct and QObject properties:

// code from examples/cpp/types/myobject.hh:32

struct Mystruct
{
int a;
int b;
};

class MyQObject : public QObject
{
Q_OBJECT;
Q_PROPERTY(Mystruct mystruct READ mystruct WRITE setMystruct);
Q_PROPERTY(QMainWindow* mainwindow READ mainwindow WRITE setMainwindow);

The following code shows how to write conversion handler class to handle the Mystruct C++ type as a table value on the lua side:

// code from examples/cpp/types/myobject.hh:41

QTLUA_METATYPE(MyStructConvert, Mystruct);

QtLua::Value MyStructConvert::qt2lua(QtLua::State *ls, const Mystruct *qtvalue)
{
QtLua::Value luavalue(QtLua::Value::new_table(ls));
luavalue[1] = qtvalue->a;
luavalue[2] = qtvalue->b;
return luavalue;
}

bool MyStructConvert::lua2qt(Mystruct *qtvalue, const QtLua::Value &luavalue)
{
qtvalue->a = luavalue.at(1);
qtvalue->b = luavalue.at(2);
return true;
}

The conversion handler and the Qt meta type will be registered on class instantiation:

// code from examples/cpp/types/meta.cc:30

MyStructConvert mystruct_converter;

Moreover, a template class with builtin conversion functions is available to readily handle QObject pointer cases:

// code from examples/cpp/types/meta.cc:33

MyQMainwindowConvert qmainwindow_converter;

See also qRegisterMetaType, QTLUA_METATYPE macro, QTLUA_METATYPE_ID macro and QTLUA_METATYPE_QOBJECT macro.

Members  

Protected functions  

Macros  

Members detail  

MetaType(int type)  

This member access is protected.

Register a conversion handler for an already registered Qt meta type.

MetaType(const char *type_name)  

This member access is protected.

Register a conversion handler and qt meta type (using the qRegisterMetaType function).

~MetaType()  

This member access is protected.

Unregister conversion handler

#define QTLUA_METATYPE(name, typename_)  

This macro defines a type conversion handler class. Class instantiation will also take care of registering the meta type to Qt.

This macro expands to:

struct name
: public QtLua::MetaType<typename_>
{
name()
: QtLua::MetaType<typename_>(#typename_) {}

inline QtLua::Value qt2lua(QtLua::State *ls,
typename_ const * qtvalue);
inline bool lua2qt(typename_ *qtvalue,
const QtLua::Value &luavalue);
};

See also MetaType class.

#define QTLUA_METATYPE_ID(name, typename_, typeid_)  

This macro defines a type conversion handler class for an existing Qt meta type id.

This macro expands to:

struct name
: public QtLua::MetaType<typename_>
{
name()
: QtLua::MetaType<typename_>((int)typeid_) {}

inline QtLua::Value qt2lua(QtLua::State *ls,
typename_ const * qtvalue);
inline bool lua2qt(typename_ *qtvalue,
const QtLua::Value &luavalue);
};

See also MetaType class.

#define QTLUA_METATYPE_QOBJECT(name, class_)  

This macro defines a type conversion class along with its handler functions able to convert QObject pointers values. The class_ parameter is the bare class name without star.

See also MetaType class.

virtual bool lua2qt(X *qtvalue, const Value &luavalue) = 0;  

This member access is protected.

This function must be implemented to converts a lua value to a C++ value.

The return value is true on success.

virtual Value qt2lua(State *ls, const X *qtvalue) = 0;  

This member access is protected.

This function must be implemented to converts a C++ value to a lua value.

Valid XHTML 1.0 StrictGenerated by diaxen on Sat Mar 30 15:29:54 2013 using MkDoc