Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. from c++ QVector<QVector<QString>> to QML JS array
Forum Updated to NodeBB v4.3 + New Features

from c++ QVector<QVector<QString>> to QML JS array

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 393 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    shokarta
    wrote on last edited by
    #1

    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
        }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Well, one issue from the code you posted is that your method has no parameter while your QML call passes two.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      JonBJ 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Well, one issue from the code you posted is that your method has no parameter while your QML call passes two.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @SGaist
        Yes, but it outputs qDebug() << "finished!"; at the end so one assumes it is executing.

        C++ function returns QVector<QVector<QString>>. Code says return result in table is undefined. Do you have to do some meta-registration thingie to pass types between C++/Qt and QML/JS?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          shokarta
          wrote on last edited by shokarta
          #4

          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

          1 Reply Last reply
          0
          • S shokarta has marked this topic as solved on

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved