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. Updating ListView when StringList in another class has changed
Qt 6.11 is out! See what's new in the release blog

Updating ListView when StringList in another class has changed

Scheduled Pinned Locked Moved QML and Qt Quick
1 Posts 1 Posters 1.4k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hello everyone :)

    I'm starting right now in Qt/QML developing and I have a simple question. I have the QML page "listStandings" with the following ListView:
    @ListView {
    id: liveStandingsListPositions
    anchors.fill: parent
    interactive: false
    model: livePositions
    delegate:
    Item {
    id: liveStandingsListPositionsItem
    height: 30
    width: 30
    anchors.left: parent.left
    anchors.leftMargin: 10
    Text {
    color: "white"
    text: modelData

                            }
                        }
                }@
    

    The model "livePositions" is from the class live.h and updated through the replyFinished slot:
    @class Live : public QObject
    {
    Q_OBJECT
    private slots:
    void replyFinished(QNetworkReply* pReply) {
    ...
    positions = tempPos;
    driver = tempDriver;
    gaps = tempGap;
    }
    public:
    explicit Live(QObject *parent = 0) : QObject(parent) {}

    QNetworkAccessManager* m_manager;
    QTimer *timer; // = new QTimer(this);
    QStringList positions;
    QStringList driver;
    QStringList gaps;
    

    ...
    }@

    And enabled to QML in the main.cpp:
    @{
    QScopedPointer<QApplication> app(createApplication(argc, argv));

    //Definiere neues Objekt
    TimeChanges timeChangesObj;
    Live liveObj;
    QmlApplicationViewer viewer;
    
    liveObj.init();
    liveObj.fetch();
    
    timeChangesObj.dst = false;
    
    //Objekt den QML-Dateien bekannt machen
    viewer.rootContext()->setContextProperty("timeChanges", &timeChangesObj);
    viewer.rootContext()->setContextProperty("live", &liveObj);
    viewer.rootContext()->setContextProperty("livePositions", QVariant::fromValue(liveObj.positions));
    viewer.rootContext()->setContextProperty("liveDriver", QVariant::fromValue(liveObj.driver));
    viewer.rootContext()->setContextProperty("liveGaps", QVariant::fromValue(liveObj.gaps));
    
    viewer.setMainQmlFile&#40;QLatin1String("qml/F1uptodate/main.qml"&#41;&#41;;
    viewer.showExpanded(&#41;;
    
    return app->exec&#40;&#41;;
    

    }@

    I found out that I have to call setContextProperty() every time, when the string list gets updated.
    But how do I call this method from live.h?

    Best Regards,
    Sebastian

    EDIT: Solved by using Q_PROPERTY:
    Q_PROPERTY(QStringList _positions READ _positions NOTIFY liveListChanged)

    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