ListView doesn't update after changing data in QStringListModel



  • Hi!

    I'm writing a program to show in real time the position of the planets in the ecliptic.

    First, I have a ListView:

    @
    import QtQuick 2.0

    Item {
    width: 200
    height: 400

    ListView {
        width: parent.width; height: 300
        id: listView
        anchors.fill: parent
        highlight: Rectangle { color: "lightsteelblue"; radius: 5 }
        focus: true
        model: myModel
        delegate: Rectangle {
            height: 20
            width: parent.width
            Text {
                text: display
                font.pointSize: 12
            }
        }
    }
    

    }
    @

    Then, I have a QStringListModel:
    @
    class QalPlanetGroup;
    class QStringListModel;

    #include "qtquick2applicationviewer.h"

    class Viewer : public QtQuick2ApplicationViewer
    {
    Q_OBJECT
    public:
    Viewer();

    public slots:
    void update();

    private:
    QalPlanetGroup *planetGroup;
    QStringListModel *model;
    };
    @

    As you can see in the following code, the data updates every second and every row is checked. If the position of the planet has changed, it sets the model's row data. The problem is that the ListView doesn't update, it keeps showing the same initial data, and I don't know what to change to make it update it. In fact, I have the same program made of QWidgets and works perfectly well, but I want to port it to QML to be able to use it in my mobile phone.

    @
    #include <QStringListModel>
    #include <QQuickView>
    #include <QQmlContext>
    #include <QTimer>

    #include <qalplanetgroup.h>

    #include "viewer.h"

    #include <QDebug>

    Viewer::Viewer()
    {
    model = new QStringListModel;
    planetGroup = new QalPlanetGroup;
    planetGroup->addMainPlanets();
    planetGroup->computeData();

    QDateTime dateTime = QDateTime::currentDateTimeUtc();
    
    planetGroup->computeData(dateTime);
    QStringList l = planetGroup->toStringList("%n:\t

  • Moderators

    This is expected behaviour. See the "documentation":http://qt-project.org/doc/qt-5.0/qtquick/qtquick-modelviewsdata-cppmodels.html:
    [quote]Note: There is no way for the view to know that the contents of a QStringList have changed. If the QStringList changes, it will be necessary to reset the model by calling QQmlContext::setContextProperty() again.[/quote]



  • Hi sierdzio,

    That's true for QStringList-based models and QObjectList-based models. But in this case I'm dealing with QStringListModel, a QAbstractItemModel subclass. In the third section of the page you mention:

    bq. QML views are automatically updated when the model changes. Remember the model must follow the standard rules for model changes and notify the view when the model has changed by using QAbstractItemModel::dataChanged(), QAbstractItemModel::beginInsertRows(), and so on. See the Model subclassing reference for more information.

    The thing is that the model automatically notifies the changes (as a QAbstractItemModel subclass) but for some reason it doesn't take effect.

    Thank you for your reply, anyway :-)


Log in to reply
 

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