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. [Solved] C++ QVariantList in QML : view in QML is not updated when I change data in items ?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 3 Posters 2.8k 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.
  • S Offline
    S Offline
    serdef
    wrote on last edited by
    #1

    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 ?

    tnx
    serdef

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bercik
      wrote on last edited by
      #2

      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.

      Cheers

      1 Reply Last reply
      0
      • F Offline
        F Offline
        fxam
        wrote on last edited by
        #3

        You may consider subclassing "QAbstractListModel":http://qt-project.org/doc/qt-5/qtquick-modelviewsdata-cppmodels.html#qabstractitemmodel

        1 Reply Last reply
        0
        • S Offline
          S Offline
          serdef
          wrote on last edited by
          #4

          hi bercik, fxam, thanks for the advice.

          i could make it work by forcing the signal to be emitted,
          @void MaQGate::SomethingChanged(void)
          {
          nameChanged();
          weightChanged();
          }@
          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.

          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