QAbstractListModel dataChanged() refresh not working in QML



  • 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 ?



  • @clogwog said:

    MyDataModel

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



  • @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 ));
    }
    

    }



  • 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.



  • @clogwog As expected


Log in to reply
 

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