MeVisLab Scripting Reference
MLStandardItemModelWrapper Class Reference

Inherits MLAbstractItemModelWrapper.

Public Slots

Changing the model
virtual int getChildPosition (const ModelIndexWrapper *child)
virtual void clear ()
virtual void insertItems (const ModelIndexWrapper *parent, unsigned int position, const QVariant &items)
virtual ModelIndexWrapperinsertItem (const ModelIndexWrapper *parent, unsigned int position, const QVariantMap &itemData)
virtual void removeItems (const ModelIndexWrapper *parent, unsigned int position, unsigned int numItems)
virtual void removeItem (ModelIndexWrapper *index)
- Public Slots inherited from MLAbstractItemModelWrapper
bool isFlat () const
bool hasChildren (const ModelIndexWrapper *parent)
int getChildCount (const ModelIndexWrapper *parent)
ModelIndexWrappergetChild (const ModelIndexWrapper *parent, unsigned int index)
ModelIndexWrappergetParent (const ModelIndexWrapper *child)
ModelIndexWrapperfindFirst (const QVariant &attribute, const QVariant &value)
ModelIndexWrappersearchItem (const QVariant &attribute, const QVariantList &values)
int getAttributeCount () const
QStringList getAttributeNames () const
QString getAttributeName (int index) const
QVariant getAttributeDefault (int index) const
int getAttributeIndex (const QString &name)
QVariant getData (const ModelIndexWrapper *item, int attributeIndex)
bool setData (const ModelIndexWrapper *item, int attributeIndex, const QVariant &data)
bool bulkSetData (const QList< ModelIndexWrapper * > &items, int attributeIndex, const QVariant &data)
QVariant getData (const ModelIndexWrapper *item, const QString &attributeName)
bool setData (const ModelIndexWrapper *item, const QString &attributeName, const QVariant &data)
bool bulkSetData (const QList< ModelIndexWrapper * > &items, const QString &attributeName, const QVariant &data)


void childrenRequest (ModelIndexWrapper *item)
- Signals inherited from MLAbstractItemModelWrapper
void itemChanged (ModelIndexWrapper *item, bool after)
void itemsInserted (ModelIndexWrapper *parent, unsigned int childIndex, unsigned int itemsInserted, bool after)
void itemsRemoved (ModelIndexWrapper *parent, unsigned int childIndex, unsigned int itemsRemoved, bool after)
void dataChanged (const QList< ModelIndexWrapper * > &items, const QList< int > &attributeIndices)

Static Public Member Functions

static ml::RefCountedBase * createObject (const QVariantList &arguments)

Detailed Description

This class wraps the class ml::StandardItemModel and thus provides an item model that can be created from scripting.

For a working example on how to use this to fill a self-defined model dynamically, have a look at the source code of the FileSystemItemModelData and the FileSystemItemModelView macro modules.

Member Function Documentation

void MLStandardItemModelWrapper::childrenRequest ( ModelIndexWrapper item)

The children of an item are requested for the first time. This can be used to add direct children of this item dynamically when they are requested with insertItems (which will generate no itemsInserted signal for this very item).


# after creating model:
def provideChildren(itemIndex):
# you need a way to associate the abstract itemIndex with your real objects
obj = getObjectForIndex(itemIndex)
p = 0
for child in obj.children:
# it would be smart to add items in greater batches, not one by one
myModel.insertItems(itemIndex, p, createItemDictForObject(child))
p += 1
virtual void MLStandardItemModelWrapper::clear ( )

clear the complete model (the attributes are still the same, though)

static ml::RefCountedBase* MLStandardItemModelWrapper::createObject ( const QVariantList &  arguments)

Create new item model; this is used by MLAB.createMLBaseObject to create a StandardItemModel. This takes a list with 1 to 3 arguments.

The first, required element is the list of attribute names.

The second, optional element is the index or name of an attribute indicating if an item might have children; if set, children can be added dynamically by listening to the childrenRequest signal. Use -1 if you don't need this feature and want to specify the third argument.

The third, optional element is a bool. If it is set to True, it is a hint that the model will have no sub-items. This can be used by views to avoid reserving space for sub-item indicators.


myModel = MLAB.createMLBaseObject("StandardItemModel",
[["name", "size", "directory", "writable"], "directory", False])
virtual int MLStandardItemModelWrapper::getChildPosition ( const ModelIndexWrapper child)

Get the position index of the given child relative to its parent.

virtual ModelIndexWrapper* MLStandardItemModelWrapper::insertItem ( const ModelIndexWrapper parent,
unsigned int  position,
const QVariantMap &  itemData 

Similar as above, but only one object can be inserted. This method returns the ModelIndex of the newly inserted item, which can be used for further operations.

virtual void MLStandardItemModelWrapper::insertItems ( const ModelIndexWrapper parent,
unsigned int  position,
const QVariant &  items 

insert new items, this can take a dictionary or a list of dictionaries, e.g.

# insert one item:
myModel.insertItems(currentIndex, pos, {"name": aName, "size": asize})


# insert three items:
myModel.insertItems(currentIndex, pos, [
{"name": aName1, "size": asize2},
{"name": aName2, "size": asize2, "writable": true},
{"name": aName3}

Note that you may only use keys that were provided when creating the model.

virtual void MLStandardItemModelWrapper::removeItem ( ModelIndexWrapper index)

Convenience method, removes the given item from the model, e.g.


Note that the index will of course be invalid afterwards.

virtual void MLStandardItemModelWrapper::removeItems ( const ModelIndexWrapper parent,
unsigned int  position,
unsigned int  numItems 

remove items, e.g.

myModel.removeItems(currentIndex, pos, 1)