from c++ QVector<QVector<QString>> to QML JS array
-
hello all,
i have created invokable QVector<QVector<QString>> in C++, which returns this array:
// 2D array declaration logic QVector<QVector<QString>> CreateTableau(int sizeX, int sizeY) { QVector<QVector<QString>> result; for (int idx1 = 0; idx1 < sizeX; idx1++) { result.append(QVector<QString>()); for (int idx2 = 0; idx2 < sizeY; idx2++) { result[idx1].append(QString()); } } return result; } QVector<QVector<QString>> PUB::tablePopulate() { QVector<QVector<QString>> result; for (i=0; i<tabLen; i++) { // declare colLen = amount of Columns int colLen = 20; if (i==0) { result = CreateTableau(tabLen, colLen); } //by first row we find out how many columns are there, and will declare 2D array accordingly // loop throught columns for (int j = 0; j < colLen; j++) { result[i][j] = "something"; } } qDebug() << "finished!"; return result; }
so the question is how do i load it as JS array in QML?
I tried:Component.onCompleted: { let table = []; table = pub.tablePopulate("5506", "6104"); // this properly debugs "finished!" from c++, but hod do i convert it into js array "table"? console.log(table.length); // TypeError: Cannot read property 'length' of undefined }
-
Hi,
Well, one issue from the code you posted is that your method has no parameter while your QML call passes two.
-
Hi,
Well, one issue from the code you posted is that your method has no parameter while your QML call passes two.
@SGaist
Yes, but it outputsqDebug() << "finished!";
at the end so one assumes it is executing.C++ function returns
QVector<QVector<QString>>
. Code says return result intable
isundefined
. Do you have to do some meta-registration thingie to pass types between C++/Qt and QML/JS? -
ugh... you guys are right... i was thinking very wrong way of the method...
basicaly it looks this way:
// 2D array declaration logic QVector<QVector<QString>> CreateTableau(int sizeX, int sizeY) { .... code in initial post return result; } void SAP::materials(QString lageort, QString werk) { ... not important code table_Quantities(tableHandle); } QVector<QVector<QString>> SAP::table_Quantities(RFC_TABLE_HANDLE returnTable) { for (i=0; i<tabLen; i++) { int colLen = 10; if (i==0) { result = CreateTableau(tabLen, colLen); } //by first row we find out how many columns are there, and will declare 2D array accordingly // loop throught columns for (int j = 0; j < colLen; j++) { result[i][j] = something; //qDebug() << result[i][j]; // works fine } } return result; }
and when calling it from QML:
Component.onCompleted: { let table = []; table = sap.tablePopulate("5506", "6104"); console.log(table.length); // TypeError: Cannot read property 'length' of undefined }
then it obviously is undefined when im calling sub method which creates no data unless its called from materials() :D
so I need the SAP::materials() declare also as QVector<QVector<QString>>so now it works as expected...
Thank you guys
-