How to push values to QML property variant two dimensional array - dynamically?
This is what I have tried:
Code:
import QtQuick 2.0
Rectangle
{
property variant twoDimTempArray: [[]]
property variant oneDArray: [1,2,3]
MouseArea
{
anchors.fill: parent
onClicked:
{
twoDimTempArray.push (oneDArray)
twoDimTempArray[0].push (oneDArray)
twoDimTempArray[0][0] = oneDArray[0]
console.log (twoDimTempArray)
}
}
}
They all results in .
How to push values in QML property variant two dimensional array?
Re: How to push values to QML property variant two dimensional array - dynamically?
What exactly are you trying to do?
Cheers,
_
Re: How to push values to QML property variant two dimensional array - dynamically?
I want to pass this 2 dimentional array from QML to C++ after filling some dynamically calculated values in it.
Re: How to push values to QML property variant two dimensional array - dynamically?
Why don't you create and calculate in C++ directly?
It seems you are trying to solve a problem that doesn't exist in a properly designed program.
Can you give a real world example instead of an abstract explanation?
Cheers,
_
Re: How to push values to QML property variant two dimensional array - dynamically?
Quote:
Originally Posted by
anda_skoa
Why don't you create and calculate in C++ directly?
This question does not make much sense to me.
I have a GUI designed in QML. User is supposed to fill in some values there.
I am supposed to calculate something from those values and then pass them to C++ so that they can be used
in an algorithm.
You think this kind of problem cannot exist in a properly designed program?
Anyways,
I have found a temporary solution as follows:
One way to add the values dynamically to a 1 dimensional QML variant is to fill a normal Javascript array and then assign it to the QML variant.
Code:
import QtQuick 2.0
Rectangle
{
property variant oneDArray: []
MouseArea
{
anchors.fill: parent
onClicked:
{
var t = new Array (0)
t.push(11)
t.push(12)
oneDArray = t
console.log (oneDArray)
}
}
}
Output:
Code:
Starting /home/.../documents/test/build-junk-Desktop_Qt_5_1_0_GCC_64bit-Debug/junk...
QML debugging is enabled. Only use this in a safe environment.
[11,12]
/home/.../documents/test/build-junk-Desktop_Qt_5_1_0_GCC_64bit-Debug/junk exited with code 0
Re: How to push values to QML property variant two dimensional array - dynamically?
Quote:
Originally Posted by
TheIndependentAquarius
This question does not make much sense to me.
The question if you can provide a real world example?
Quote:
Originally Posted by
TheIndependentAquarius
I have a GUI designed in QML. User is supposed to fill in some values there.
So far so goo.
Quote:
Originally Posted by
TheIndependentAquarius
I am supposed to calculate something from those values and then pass them to C++ so that they can be used
in an algorithm.
Why don't you simply pass the values from the UI?
What makes it necessary to do calculations in the UI?
Quote:
Originally Posted by
TheIndependentAquarius
You think this kind of problem cannot exist in a properly designed program?
No. But in a lot of cases strange requirements for QML side code are hints that the program does things in the UI context that would better be done in the application's non-UI parts.
Quote:
Originally Posted by
TheIndependentAquarius
One way to add the values dynamically to a 1 dimensional QML variant is to fill a normal Javascript array and then assign it to the QML variant.
Which also has the advantage of changing the value, meaning property bindings get updated.
Cheers,
_
Re: How to push values to QML property variant two dimensional array - dynamically?
Quote:
Originally Posted by
anda_skoa
Why don't you simply pass the values from the UI?
I have several points on the UI. I want the user to select some points from them.
I need to calculate the distance between the points selected by the user.
This part can be done in C++ for sure,
but I still will have to `push` the points selected by the user in a `variant` and then pass the variant to C++.
Won't I?
Re: How to push values to QML property variant two dimensional array - dynamically?
Depends on how you get these points in the first place.
For example if they originate in C++, e.g. as data accessed through a model, then selection could also be tracked there (state less UI).
Or the selection could be tracked in QML and passed to C++ when done.
But yes, if the values originate in QML then you'll need to pass them to C++.
Either using a C++ based property or a function call. The latter could take a list or each point individually.
Which is why I was asking for the actual use case really early.
It is usually better to find a solution to the actual problem and not to some abstract, academic one :)
Cheers,
_