3

if i have a class that contains a QList of listmodels (QList), how would i assign a model from that list to a listview in QML?

class code:

class TreeModel : public QAbstractItemModel { Q_OBJECT Q_PROPERTY(QQmlListProperty<ListModel> lists READ lists) public: enum AnimalRoles { TypeRole = Qt::UserRole + 1, }; explicit TreeModel(const QString &data, QObject *parent = 0); ~TreeModel(); QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE; Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE; QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; QModelIndex parent(const QModelIndex &index) const Q_DECL_OVERRIDE; int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; QStringList getGroups(); QStringList getFoldersByGroup(const QString &name); QStringList getFolderlistByGroupID(QStringList &name); void getFolders(); void populateFrontPage(); QQmlListProperty<ListModel> lists(){ return QQmlListProperty<ListModel>(this, foldermodels); } ******************************************** ListModel *groupmodel; QList<ListModel*> foldermodels; QList<ListModel*> filemodels; 

now how would i assign for example, foldermodels.at(0) to a listview in qml? i have tried stuff like:

 ListView { id: folderlist model: treemodel.lists // treemodel.lists.at(0) // treemodel.lists[0] delegate: folderDelegate contentHeight: contentItem.childrenRect.height height: childrenRect.height anchors.left: parent.left anchors.right: parent.right clip: true } 

but i am getting errors like:

QMetaProperty::read: Unable to handle unregistered datatype 'QQmlListProperty<ListModel>' for property 'TreeModel::lists' QQmlExpression: Expression qrc:/main.qml:54:28 depends on non-NOTIFYable properties: TreeModel::lists QMetaProperty::read: Unable to handle unregistered datatype 'QQmlListProperty<ListModel>' for property 'TreeModel::lists' QQmlExpression: Expression qrc:/main.qml:54:28 depends on non-NOTIFYable properties: TreeModel::lists 

and yes i have registered the Treemodel class containing the QList.

i also know the QList is actually populated with the correct models because the view shows the items when i do it like this in main.cpp

 TreeModel model(infile.readAll()); ListModel *foldermodel = model.foldermodels.at(1) // or (0) ctxt->setContextProperty("treemodel", &model); ctxt->setContextProperty("foldermodel", foldermodel); 

Thanks in advance for the help, i really apreciate it!!

more progress:

i added this line to my main.cpp

qmlRegisterType<QQmlListProperty<ListModel> >("ListMode",1,0,"listmod"); 

now i have 2 new errors:

C:\Qt\5.4\mingw491_32\include\QtQml\qqml.h:83: error: 'staticMetaObject' is not a member of 'QQmlListProperty<ListModel>' const char *className = T::staticMetaObject.className(); \ C:\Qt\5.4\mingw491_32\include\QtQml\qqml.h:244: error: 'staticMetaObject' is not a member of 'QQmlListProperty<ListModel>' uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject, 
1
  • Please don't "hijack" an existing question with a new one. Open a new question and ask it there! Commented Feb 24, 2015 at 18:59

2 Answers 2

6

in my main.cpp i added the line:

qmlRegisterType<ListModel>(); 

and now it works, i am able to use ListModel objects in QML through QQmlListProperty

the code looks like:

treemodel.cpp

class ListModel class TreeModel : public QAbstractItemModel { Q_OBJECT Q_PROPERTY(QQmlListProperty<ListModel> folderLists READ folderLists) Q_PROPERTY(QQmlListProperty<ListModel> fileLists READ fileLists) Q_PROPERTY(QQmlListProperty<ListModel> getFileAttributes READ getFileAttributes) Q_PROPERTY(QQmlListProperty<ListModel> getFileUserAndDate READ getFileUserAndDate) ... QQmlListProperty<ListModel> folderLists(){ return QQmlListProperty<ListModel>(this, foldermodels); } QList<ListModel*> foldermodels; } 

main.cpp

TreeModel model; QQuickView view; view.setResizeMode(QQuickView::SizeRootObjectToView); QQmlContext *ctxt = view.rootContext(); qmlRegisterType<ListModel>(); ctxt->setContextProperty("treemodel", &model); view.setSource(QUrl(QStringLiteral("qrc:/main.qml"))); view.show(); return app.exec(); 

oh and for completeness sake,

in qml you can call the listmodel like:

ListView { id: folderlist model: treemodel.folderLists[treemodel.modIndex] delegate: folderDelegate contentHeight: contentItem.childrenRect.height height: childrenRect.height anchors.left: parent.left anchors.right: parent.right clip: true spacing: 3 } 

the modIndex function returns an integer for iterating the QList list.

hope this helps someone out

Sign up to request clarification or add additional context in comments.

Comments

1
Unable to handle unregistered datatype 'QQmlListProperty<ListModel> 

This means that QML does not know the type of the property. You need to register it with Q_DECLARE_METATYPE:

Q_DECLARE_METATYPE(QQmlListProperty<ListModel>) 

1 Comment

Thanks alot, i added the line: qmlRegisterType<QQmlListProperty<ListModel> >("ListMode",1,0,"listmod"); i have 2 new errors now

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.