[SOLVED]QVariantList to QML
QML and Qt Quick
4
Posts
2
Posters
10.0k
Views
1
Watching
-
Hi all,
I want to pass a list from C++ to a repeater in QML. To do so I created a methode converting my list to a qvariant list:Q_INVOKABLE QVariantList getPagesForChapter(int chapterID); QVariantList CPagesList::getPagesForChapter(int chapterID){ QList<QVariant>* pages = new QList<QVariant>(); foreach (CPage* page, m_pagesCleared) { if (page->chapterID() == chapterID){ pages->append(QVariant::fromValue(page)); } } qDebug() << "pages for chapter: " << chapterID << " count: " << pages->size(); return *pages; }
Within my qml:
Repeater { id: pagesPortraitList model: pages.getPagesForChapter(currentPage.chapterID) delegate: Item { id: singleItem width: portraitPagesColumn.width height: portraitPagesColumn.height / 3 Image { id: mainViewImageTop source: "qrc:/images/images/data/" + model.picture }
Doing this I get following error:
Unable to assign [undefined] to QString
What am I doing wrong? Or what is the best way to pass a changing list to qml (without storing the list in a field of my class and than using QMLListProperty)
thx,
patrik -
Use QStringList instead of QVariantList
pages->append(page->getPicture());
Use modelData role instead of model
Image { id: mainViewImageTop source: "qrc:/images/images/data/" + modelData }
Read more here http://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html