How can QML get a 2D double array filled by Q_INVOKABLE C++ member function?
-
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! -
It looks like you are missing a "return result" in getLightPosition
-
It looks like you are missing a "return result" in getLightPosition
@Jonas-Karlsson You are exactly right - thank you!
Why didn't the compiler spot that error?? -
@Jonas-Karlsson You are exactly right - thank you!
Why didn't the compiler spot that error??