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 elements in a repeater?
Forum Updated to NodeBB v4.3 + New Features

updating elements in a repeater?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
43 Posts 5 Posters 14.0k Views 4 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.
  • mzimmersM mzimmers

    I know, I'm doing a lousy job of explaining this. in C++:

    typedef QVector<Bottle> Bottles;
    class ReagentManager : public QObject {
      Q_OBJECT
     private:
      Bottles m_bottleList;
    }
    <in another file>
      ReagentManager m_reagentManager;
      engine->rootContext()->setContextProperty("reagentManager", &m_reagentManager);
    

    In QML:

        onVisibleChanged: {
          if (visible) {
            reagentManager.updateBottleList()
            rack.updateBottles()
          }
        }
    
        // update our QML array based on the C++ model.
        function updateBottles() {
          var modelSize = bottleModel.count
          var i
          var l_color
          var volume
          var minVolume
          var amountNeeded
          var name
    
          for (i = 0; i < modelSize; ++i) {
            name = reagentManager.getName(i)
            bottleRepeater.itemAt(i).cellText = name
    
            volume = reagentManager.m_volume
            minVolume = reagentManager.getMinVolume(i)
            amountNeeded = reagentManager.getAmountNeeded(i)
            l_color = ((volume - minVolume) >= amountNeeded) ? "green" : "red"
            bottleRepeater.itemAt(i).cellColor = l_color
          }
        }
    

    So, my QML function calls C++ routines to obtain the needed data. I'm trying to convert this to the approach you suggested; this is where I ran into the problem with the struct.

    I still don't see where the QVariantList comes into play, though.

    ODБOïO Offline
    ODБOïO Offline
    ODБOï
    wrote on last edited by ODБOï
    #41

    this is not directly related but
    @mzimmers said in updating elements in a repeater?:

    onVisibleChanged: {
    if (visible) {
    reagentManager.updateBottleList()
    rack.updateBottles()
    }
    }

    are you calling this in your Repeater delegate ? If yes, then i believe you will call this multiple times unnecessarily, one way to verify it in qml is putting a console.log() after or before this call to see how meny time this is called

    1 Reply Last reply
    1
    • ODБOïO ODБOï

      Q_INVOKABLE QVariant bottleList() { return QVariant::fromValue(m_bottleList); }

      mzimmersM Offline
      mzimmersM Offline
      mzimmers
      wrote on last edited by
      #42

      @LeLev said in updating elements in a repeater?:

      Q_INVOKABLE QVariant bottleList() { return QVariant::fromValue(m_bottleList); }

      Well, bless my buttons...that works! But why is bottleList() declared as returning a QVariant, and not a QVariantList?

      Regarding your question: the onVisibleChanged isn't in the repeater; it's in the parent Rectangle. This was my way of ensuring that the C++ data in ReagentManager updates itself whenever the view is activated.

      ODБOïO 1 Reply Last reply
      1
      • mzimmersM mzimmers

        @LeLev said in updating elements in a repeater?:

        Q_INVOKABLE QVariant bottleList() { return QVariant::fromValue(m_bottleList); }

        Well, bless my buttons...that works! But why is bottleList() declared as returning a QVariant, and not a QVariantList?

        Regarding your question: the onVisibleChanged isn't in the repeater; it's in the parent Rectangle. This was my way of ensuring that the C++ data in ReagentManager updates itself whenever the view is activated.

        ODБOïO Offline
        ODБOïO Offline
        ODБOï
        wrote on last edited by
        #43

        @mzimmers said in updating elements in a repeater?:

        Well, bless my buttons...that works!

        Nice :)

        @mzimmers said in updating elements in a repeater?:

        But why is bottleList() declared as returning a QVariant, and not a QVariantList?

        I don't have a technical explanation for that really, im used to do that since i saw it somewhere in the docs. But i guess you could return a QVariantList directly. It will be converted to javascript array as explained here https://doc.qt.io/qt-5/qtqml-cppintegration-data.html

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved