简体   繁体   English

将2d数组从c ++传递到qml

[英]Pass 2d Array from c++ to qml

The question is above. 问题在上面。 I can create a 2d array in qml like this: 我可以在qml中创建一个二维数组,如下所示:

function create()
{
    var array= new Array(9);
    array[0]= new Array(
}

So how I can create such array in c++? 那么如何在c ++中创建这样的数组呢? I tried: 我试过了:

QVariant myArray= QVariant([4,5,6,7]);

but this doesn't work. 但这不起作用。

You can use QVariantList which could be passed to qml: 您可以使用可以传递给qml的QVariantList

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

The problem is: QVariant cannot store arrays, so this lines won't compile at all: 问题是: QVariant无法存储数组,因此以下行根本不会编译:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM