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. QAbstractListModel dataChanged() refresh not working in QML
Forum Updated to NodeBB v4.3 + New Features

QAbstractListModel dataChanged() refresh not working in QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qabstractlistmoqmlrefresh
5 Posts 2 Posters 9.7k 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.
  • clogwogC Offline
    clogwogC Offline
    clogwog
    wrote on last edited by A Former User
    #1

    In my main.cc i set a data-model to a

    MyDataModel aDataModel;  // MyDataModel is a subclass of QAbstractListModel
    ctxt->setContextProperty("aaModel", &aDataModel);
    

    and in the QML i use it as the model like:

      ListView {
            model: aaModel
           delegate: Item { ...
    

    this works fine when the QML is initially loaded, but when the data is updated it doesn't get re-evaluated and i still see the old data until i pop out of the QML and re-load.

    when the data gets updated in the model i emit dataChanged(..) without avail.

    any tips on how i can get the refresh can happen ?

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jalomic
      wrote on last edited by
      #2

      @clogwog said:

      MyDataModel

      Can you show some your code of this class ? especially data changing part

      clogwogC 1 Reply Last reply
      0
      • J jalomic

        @clogwog said:

        MyDataModel

        Can you show some your code of this class ? especially data changing part

        clogwogC Offline
        clogwogC Offline
        clogwog
        wrote on last edited by
        #3

        @jalomic

        of coarse:

        // called when a REST function returns actual data
        void MyDataModel::recentPhotoRequestDone()
        {
        QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
        QByteArray data = reply->readAll();
        QDomDocument doc;
        doc.setContent(data);
        QDomNodeList nodes = doc.elementsByTagName("photo");

            image_list.clear();
            for(int t=0; t < nodes.count(); t++ )
            {
                QDomElement photo = nodes.at(t).toElement();
               if( datavalidchecks )
                {
                    QString photoUrl = QString("http://farm%1.staticflickr.com/%2/%3_%4_q.jpg").arg(photo.attribute("farm"),photo.attribute("server"),photo.attribute("id"),photo.attribute("secret"));
                    TSImageData image1(QUrl(photoUrl) );
                    if( photo.hasAttribute("title"))
                        image1.setName(photo.attribute("title"));
                        image_list.append(image);
                }
            }
            int end1 = image_list.count();
            emit dataChanged (index(0),index(end1 ));
        }
        

        }

        1 Reply Last reply
        0
        • clogwogC Offline
          clogwogC Offline
          clogwog
          wrote on last edited by clogwog
          #4

          found the answer here http://www.cplusplusdevelop.com/91_21846173/

          instead of calling
          emit dataChanged(...)

          i now use beginInsertRows and endInserRows and it all starts working.

          thanks @jalomic your question made me search more.

          J 1 Reply Last reply
          0
          • clogwogC clogwog

            found the answer here http://www.cplusplusdevelop.com/91_21846173/

            instead of calling
            emit dataChanged(...)

            i now use beginInsertRows and endInserRows and it all starts working.

            thanks @jalomic your question made me search more.

            J Offline
            J Offline
            jalomic
            wrote on last edited by
            #5

            @clogwog As expected

            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