Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How can QML get a 2D double array filled by Q_INVOKABLE C++ member function?
Forum Updated to NodeBB v4.3 + New Features

How can QML get a 2D double array filled by Q_INVOKABLE C++ member function?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 3 Posters 203 Views 2 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.
  • Tom assoT Offline
    Tom assoT Offline
    Tom asso
    wrote last edited by Tom asso
    #1

    I'm developing with Qt 6.8.2 on ubuntu 22.04.
    QML code should call a Q_INVOKABLE member function of a C++ object; the function should return a 1-dimensional double array. Based on advice from Claude.ai I use the following approach, but QML encounters a seg-fault when it attempts to access the returned double array:
    C++:

    class MyClass : public QObject {
        Q_OBJECT
      public:
        /// Return the x-y-z position
        Q_INVOKABLE QVariantList getLightPosition(void) {
        double position[3] = {1., 2., 3.};
        QVariantList result;
      
        for (int i = 0; i < 3; i++) {
          qDebug() << "getLightPosition(): " << i << ": " << position[i];
          result.append(position[i]);
        }
      }
    }
    

    QML:

    Dialog {
       [...]
        onOpened: { console.log('settings3dDialog opened');
                    // Set values in settings gui to current values
                    var pos = topoDataItem.getLightPosition()
    		console.log('pos[0]=', pos[0])		
          }
    }
    

    How to properly do this?
    Thanks!

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jonas Karlsson
      wrote last edited by
      #2

      It looks like you are missing a "return result" in getLightPosition

      Tom assoT 1 Reply Last reply
      2
      • J Jonas Karlsson

        It looks like you are missing a "return result" in getLightPosition

        Tom assoT Offline
        Tom assoT Offline
        Tom asso
        wrote last edited by
        #3

        @Jonas-Karlsson You are exactly right - thank you!
        Why didn't the compiler spot that error??

        SGaistS 1 Reply Last reply
        0
        • Tom assoT Tom asso

          @Jonas-Karlsson You are exactly right - thank you!
          Why didn't the compiler spot that error??

          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote last edited by
          #4

          @Tom-asso hi,

          Check the build log, you likely have a warning about that issue from the compiler.

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

          1 Reply Last reply
          0

          • Login

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