[SOLVED]QVariantList to QML



  • 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



  • Hi Beemaster,
    I can't, the picture is only a part of more data i use from the class in the delegate.
    thx,
    patrik



  • hi beemaster,
    the tip with the modelData helped.
    using:
    model.modelData.picture
    instead of:
    model.picture
    worked.
    thx,
    patrik


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.