Updating ListView when StringList in another class has changed



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


Log in to reply
 

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