updating elements in a repeater?
-
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.
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
-
@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.
-
@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.
@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