[Solved] C++ QVariantList in QML : view in QML is not updated when I change data in items ?

  • Hi,

    I am making a data exchange between C++ (JSON RPC server with some additional logic) and QML.

    For a list of complex objects, I store them in a C++ QMap,
    and construct a QVariantList from that.
    This list is used as a model for a grid view in QML, with bindings to the item properties.
    It basically works but the visual updating only happens in some cases :

    The QVariant list consists of objects with public properties such as
    @ Q_PROPERTY(long weight READ getweight WRITE setweight NOTIFY weightChanged)@

    The list is exposed to QML after EVERY change, using :
    @GUIRootContext->setContextProperty("GateList", QVariant::fromValue(myGateList));@

    Now when I change the weight poperty of an item in the list, and call this setContextProperty afterwards,
    the value is still not updated in the grid. So in that case updating/binding does not work.

    When I add an additional items to the list, the new object is added in the grid , and the previous updates are now shown too (for the other objects where i had changed properties). So in this case updating/binding it triggered.

    It seems setContextProperty does not trigger binding evaluation every time, it depends on the detected changes, and changes inside objects are not considered.

    What is the right way to make this work ?
    Is there a good way to sync an item update (for items in a list or map) from C++ to QML ?


  • Hi,
    i might be wrong because i haven't done such thing exactly yet, but i think that you should call setContextProperty only once to expose list to qml, and just emit "weightChanged" signal after every change.


  • hi bercik, fxam, thanks for the advice.

    i could make it work by forcing the signal to be emitted,
    @void MaQGate::SomethingChanged(void)
    I call SomethingChanged() after a property change,
    and the QML is synced. IT WORKS !

    This works fine for now,
    I'll have a closer look at QAbstractListModel,
    it may be a more elegant solution.

Log in to reply

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