Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [SOLVED]QVariantList to QML
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]QVariantList to QML

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 2 Posters 10.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • patrikdP Offline
    patrikdP Offline
    patrikd
    wrote on last edited by patrikd
    #1

    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

    1 Reply Last reply
    0
    • B Offline
      B Offline
      beemaster
      wrote on last edited by beemaster
      #2

      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

      1 Reply Last reply
      1
      • patrikdP Offline
        patrikdP Offline
        patrikd
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • patrikdP Offline
          patrikdP Offline
          patrikd
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved