Qlist of qlist
Hi everybody, I created qvariantlist of qvariantlist like this:
@ QVariantList DP;
DP<< 2 << 2300 << 400 << 14.5 << 17.62 << 0 << 0 << 0 << 80;
QVariantList DC; DC<< 3 << 2700 << 35 << 0 << 21.6 << 21.6 << 0 << 2735 << 0; QVariantList dataList; dataList<< DP <<DC ;@
but my problem is that, when i return the size of the datalist (@qDebug() << dataList.size();@), qdebug shows the number 16, but I want to show 2, because i filled it up with two data :(
In your program third list is not declared properly. I think your idea is to create list of QVariantList. It should be something like the following. Try following.
@ QList<QVariantList> dataList;
qDebug() << dataList.size();@
thanks, it worked. but if i want to pass it to qml what do i do?
i use it:
@ QQuickView view;
QQmlContext *myList = view.rootContext(); myList->setContextProperty("myList", QVariant::fromValue(dataList)); view.setSource(QUrl("Main.qml")); view.show();@
but in qml i can't use it, for example @myList @
Did you get a chance to look at QQmlListProperty ? This may help you.
is it possible to pass 2d list from c++ to qml ?
when i used @QVariantList dataList;
dataList<< DP <<DC ;@ instead of @QList<QVariantList> dataList;
dataList<<DP<<DC;@ i could to pass it with that codes but in 1d list, for example myList  worked but now it doesn't work
Guys, read the docs.
<< operator for lists appends list elements, not the lists themselves. If you want to create a list of lists you need to explicitly create a variant from the list, eg.
//this appends DP and DC elements to dataList
//size of dataList is size of DP + size of DC
dataList << DP << DC;
//this creates new variants of type "list" and puts them into dataList
//size is 2
dataList << QVariant(DP) << QVariant(DC);
Btw. QList<QVariantList> doesn't make sense. QVariantList is a typedef for QList<QVariant> so this gives QList<QList<QVariant>>, and what we want is QList<QVariant> where the inner QVariant holds another QList<QVariant>.
Thanks Chris. It is typedef as well. Based on this only I suggested him. It depends on the requirement on how to make list of list. I have used it where I wanted list of List only. Based on this only I suggested him to create QList of QVariantList. I don't want QList<Variant>.
thanks Chris and Dheerendra. i have done it.