LuaModel class reference
[Model/View module]


#include <QtLua/UserItemModel>

namespace QtLua {
class LuaModel;

This class is a member of the QtLua namespace.


This class allows defining a Qt model using lua code. Lua functions must be provided to the C++ model wrapper in order to implement the model.

The qt.mvc.new_lua_model function lua function can be used to create such a model from lua script.

read-only lua model  

At least one lua function must be provided to implement a read-only model. This mandatory function is responsible for exposing the model layout and data:

function get(role, item_id, child_row, child_col)

The get function may be first called multiple times by the wrapper with a nil value in the role parameter. In this case, the lua code must expose the data layout:

  • role is a nil value.

  • item_id is the numerical id of the queried item. 0 is reserved for root.

  • child_row and child_col are the row and column of a child item under the queried item, starting at 1.

The lua code must then return at least 3 values. This is enough if the queried item has no parent:

return (item_rows, item_cols, child_id, parent_id, item_row, item_col, flags)
  • item_rows and item_cols are the number of rows and columns under the item specified by item_id.

  • child_id is a numerical id for the child item at position specified by child_row and child_col under the queried item. A positive id must provided by the lua code and will be used to refer to this item later.

  • parent_id is the numerical id of the parent of the item specified by item_id. nil or 0 can be returned for root.

  • item_row and item_col give the position of the item specified by item_id in its parent, starting at 1. This is not used if parent_id is nil or 0.

  • flags is the Qt::ItemFlag value to use for the queried item. A default value is used if nil.

If the role parameter is not a nil value, the get function must instead return the data associated with the specified the display role:

return (item_data, data_type)

If the lua code returns two values, the second value is a numeric Qt type handle which is used to perform the type conversion from the lua the value (see qt.meta_type function). The simple ValueBase::to_qvariant function is used to perform conversion when this hint is not present.

editable lua model  

Five other functions may be provided to implement an editable model.

The set function is responsible for updating the data of an item; it must return true if the update was successful:

function set(role, item_id, value)

Four more function can be provided to support insertion and removal of items in the model:

function insert_rows(check, parent_id, pos, count)
function insert_cols(check, parent_id, pos, count)
function remove_rows(check, parent_id, pos, count)
function remove_cols(check, parent_id, pos, count)

When the check parameter value is true, the model must return a boolean value to indicate if the insert action is allowed. The insertion will takes place on the next call if the first call returns true.


Some examples of lua list model and tree model are available in the QtLua tree.




  • LuaModel(const Value &get_func, const Value &set_func = [...], const Value &insert_rows_func = [...], const Value &remove_rows_func = [...], const Value &insert_cols_func = [...], const Value &remove_cols_func = [...], QObject *parent = [...])

Members detail  

LuaModel(const Value &get_func, const Value &set_func = Value(), const Value &insert_rows_func = Value(), const Value &remove_rows_func = Value(), const Value &insert_cols_func = Value(), const Value &remove_cols_func = Value(), QObject *parent = 0)  

No documentation available

enum ItemDataRole  


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