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. ListView doesn't update after changing data in QStringListModel
QtWS25 Last Chance

ListView doesn't update after changing data in QStringListModel

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 2.5k Views
  • 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.
  • D Offline
    D Offline
    David_Gil
    wrote on last edited by
    #1

    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
    
    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      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]

      (Z(:^

      1 Reply Last reply
      0
      • D Offline
        D Offline
        David_Gil
        wrote on last edited by
        #3

        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 :-)

        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