The Qt documentation states that "Certain C++ sequence types are supported transparently in QML as JavaScript Array types", such as QVector<qreal>. This seems to work as expected, in that I can create a Q_INVOKABLE method returning a QVector<qreal> and read the values in QML/Javascript. However, if I try to pass the returned array to another C++ method which takes a QVariantList argument, the list received by the method is empty. Am I doing something wrong here?

The example below shows a method returning a length 5 array and passing it to another method, and gives the output:

qml: values.length: 5
list.size(): 0
list.size(): 3

while I would expect the second line to show "list.size(): 5". (The last line is just to show that passing a native JS array works as expected.) What is going on here? I am using Qt 5.8 on Linux. Thank you for any help or insight.

test.h:
Qt Code:
  1. #include <QVariantList>
  2. #include <QVector>
  3.  
  4. #include <iostream>
  5.  
  6. class Test : public QObject {
  7. Q_OBJECT
  8. public:
  9. Test(QObject *parent = 0) : QObject(parent) {}
  10. virtual ~Test() {}
  11.  
  12. Q_INVOKABLE QVector<qreal> getValues() const
  13. {
  14. return { 1,2,3,4,5 };
  15. }
  16.  
  17. Q_INVOKABLE void setValues(const QVariantList& list)
  18. {
  19. std::cout << "list.size(): " << list.size() << std::endl;
  20. }
  21. };
To copy to clipboard, switch view to plain text mode 

main.qml:
Qt Code:
  1. import QtQuick 2.5
  2. import QtQuick.Window 2.2
  3.  
  4. Window {
  5. visible: true
  6. width: 640
  7. height: 480
  8. title: "Hello World"
  9.  
  10. Component.onCompleted: {
  11. var values = Test.getValues();
  12. console.log("values.length: " + values.length)
  13. Test.setValues(values);
  14. Test.setValues([1,2,3]);
  15. }
  16. }
To copy to clipboard, switch view to plain text mode 

main.cpp:
Qt Code:
  1. #include <QGuiApplication>
  2. #include <QQmlApplicationEngine>
  3. #include <QQmlContext>
  4. #include "test.h"
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8. QGuiApplication app(argc, argv);
  9.  
  10. QQmlApplicationEngine engine;
  11. engine.rootContext()->setContextProperty("Test", new Test());
  12. engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
  13.  
  14. return app.exec();
  15. }
To copy to clipboard, switch view to plain text mode