Reputation: 302
The question is above. I can create a 2d array in qml like this:
function create()
{
var array= new Array(9);
array[0]= new Array(
}
So how I can create such array in c++? I tried:
QVariant myArray= QVariant([4,5,6,7]);
but this doesn't work.
Upvotes: 1
Views: 1346
Reputation: 113
Add more info about QVariantList:
if use append:
QVariantList list;
for (int i = 0; i < 100; ++i) {
list.append(QVariantList{i, rand() * sin(i)});
}
then the list received in js will be a 1d array, which makes me confused,
but if use insert:
QVariantList list;
for (int i = 0; i < 100; ++i) {
list.insert(list.end(), QVariantList{i, rand() * sin(i)});
}
it keeps the dimension, i can receive 2d array in js
Upvotes: 0
Reputation: 10057
The problem is: QVariant
cannot store arrays, so this lines won't compile at all:
int array[] = {0, 1, 2};
QVariant v = array;
or
QVariant x = {0, 1, 2};
or
QVariant x{0, 1, 2};
A specific type exists, though, so you'd be better doing:
QVariantList myArray =
{
QVariantList{4, 5, 6, 7},
QVariantList{0, "one", true}
//etc
};
and access items like:
int x = myArray[0].toList()[0].toInt();
bool y = myArray[1].toList()[2].toBool();
Upvotes: 1
Reputation: 32665
You can use QVariantList
which could be passed to qml:
QVariantList list;
list.append(QVariantList{5, 5, 6, 7});
Upvotes: 2