1
votes

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.

2

2 Answers

2
votes

You can use QVariantList which could be passed to qml:

QVariantList list;
list.append(QVariantList{5, 5, 6, 7});
1
votes

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();