Results 1 to 3 of 3

Thread: Returning QVector<qreal> from C++ to QML and back to C++ as QVariantList

  1. #1
    Join Date
    Mar 2017
    Posts
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Returning QVector<qreal> from C++ to QML and back to C++ as QVariantList

    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 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Returning QVector<qreal> from C++ to QML and back to C++ as QVariantList

    The type of "values" is likely still a QVector<qreal>, so your first call to setValues() calls it with an imcompatible type for the value.

    Have you tried changing the signature of setValues() to take the same type that getValues() returns?

    Cheers,
    _

  3. #3
    Join Date
    Mar 2017
    Posts
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Returning QVector<qreal> from C++ to QML and back to C++ as QVariantList

    Thanks for the suggestion, you are correct that if the setValues function takes a QVector<qreal>, there is no problem. However in my case the setValues function (which is actually in another class than the getValues function in the real application) needs to be able to take generic arrays, not just qreals. Changing the signature to setValues(const QVariant& variant) solved the problem, I can convert to a QVariantList using variant.value<QVariantList>() (not variant.toList(), which gives an empty list again.) But really, should there not be an error of some kind if I call a function with the wrong argument type?


    (Edited after I realized I had a Q_PROPERTY(QVariantList values WRITE setValues ...) which would "convert" the array into an empty list even though I used a QVariant argument in setValues...)
    Last edited by HÃ¥kon Enger; 17th March 2017 at 10:57.

Similar Threads

  1. Replies: 5
    Last Post: 10th October 2011, 15:26
  2. Replies: 5
    Last Post: 3rd September 2011, 00:11
  3. Replies: 0
    Last Post: 10th January 2011, 10:42
  4. How to log returning control back to the main event loop?
    By piotr.dobrogost in forum Qt Programming
    Replies: 5
    Last Post: 28th September 2009, 10:57
  5. Replies: 12
    Last Post: 14th June 2006, 10:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.