Passing Array from C++ to QML?
-
wrote on 20 Feb 2019, 09:51 last edited by
I need to pass Array of int, from C++ to QML and Vice Versa! I am confused which direction should I follow! any Advice?
-
I need to pass Array of int, from C++ to QML and Vice Versa! I am confused which direction should I follow! any Advice?
@MAthias_Va
you cannot pass an array directly to QML, but a QVariantList (see this) -
I need to pass Array of int, from C++ to QML and Vice Versa! I am confused which direction should I follow! any Advice?
wrote on 20 Feb 2019, 10:11 last edited by@MAthias_Va you can access array element which is in C++ from qml, just call the function from qml and pass the parameter of "index". The function which is in C++, make that return type as "int or float" according to your requirement, by which you can get values in qml from C++ for that you don't need to pass array to qml
-
wrote on 20 Feb 2019, 10:22 last edited by
Can anyone provide a simple Example? I couldn't understand the provided doc?
-
Can anyone provide a simple Example? I couldn't understand the provided doc?
wrote on 20 Feb 2019, 10:49 last edited byhi
@MAthias_Vahave a class with Q_PROPERTY QVariantList
... Q_PROPERTY(QVariantList arr READ getarr WRITE setarr) public: const QVariantList & getarr() const { return m_arr; } void setarr(const QVariantList & v) { m_arr = v; } private : QVariantList m_arr; ... //fill the m_arr in ctor for example ... m_arr<<"1111"<<1111; ...
set an instance of that class as contextProperty so it can be reached from QML
ThatClass bk; engine.rootContext()->setContextProperty("backend",&bk);
then use it
property variant varList Component.onCompleted : varList=backend.getarr onVarListChanged: { for (var i in varList){ console.log(i) } }
-
Exposing a
QList<int>
as a property kind of works.
It's not directly usable as a model though, but since 5.12 you can easily make it work by doing :model: Array.from(myIntList)
You can also modify it by doing
myIntList[index] = 42
ormyIntList.push(51)
(look at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array for reference) -
hi
@MAthias_Vahave a class with Q_PROPERTY QVariantList
... Q_PROPERTY(QVariantList arr READ getarr WRITE setarr) public: const QVariantList & getarr() const { return m_arr; } void setarr(const QVariantList & v) { m_arr = v; } private : QVariantList m_arr; ... //fill the m_arr in ctor for example ... m_arr<<"1111"<<1111; ...
set an instance of that class as contextProperty so it can be reached from QML
ThatClass bk; engine.rootContext()->setContextProperty("backend",&bk);
then use it
property variant varList Component.onCompleted : varList=backend.getarr onVarListChanged: { for (var i in varList){ console.log(i) } }
wrote on 20 Feb 2019, 14:22 last edited byThx @LeLev, this was really helpfull, Now I am enjoying editing my array in QML :)
-
Thx @LeLev, this was really helpfull, Now I am enjoying editing my array in QML :)
wrote on 20 Feb 2019, 14:25 last edited by@MAthias_Va Nice happy coding !
-
wrote on 16 Nov 2020, 15:36 last edited by
@LeLev Thx for the snippet of code! I was looking for something like this.
It works fine for passing the entire array from QML to C++, but not for a single element in the list.In my application I have a matrix of on/off buttons, so essentially several lists of booleans. I wonder if there is a more lightweight solution than passing the entire QVariantList each time a boolean flips value.
-
@LeLev Thx for the snippet of code! I was looking for something like this.
It works fine for passing the entire array from QML to C++, but not for a single element in the list.In my application I have a matrix of on/off buttons, so essentially several lists of booleans. I wonder if there is a more lightweight solution than passing the entire QVariantList each time a boolean flips value.
wrote on 16 Nov 2020, 15:48 last edited by@joaogatao hi,
you can have a Q_Invokable method that takes the index you want to returnQ_INVOKABLE bool getButtonIndex(int i){ //err check return arr.at(i); }
note if you bind this to a value in QML, it will not refresh automatically when you change values in the c++ side
//ex:CheckBox{ checked : myObj.getButtonIndex(1) // will not be refreshed if **m_arr** changes c++ side }
-
wrote on 16 Nov 2020, 16:53 last edited by
@LeLev and thx for fast response on an old topic :)
I just tried it and it does what I needI'm pretty new to Qt. So much to learn.
Cheers