Results 1 to 8 of 8

Thread: Need help passing data to qml

  1. #1
    Join Date
    Dec 2014
    Posts
    7
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Android

    Default Need help passing data to qml

    Hi i am having difficulty in passing a struct from cpp to qml. Can someone give me some pointers. I am using qt 5.4/64 bit under debian lenny rc1. Here is what i have been working with which was just a new dialog project with the following added. Note the code seems to work if i pass it just a string but not with a simple struct.

    my .h
    Qt Code:
    1. namespace Ui {
    2. class Dialog;
    3. struct MyStruct {
    4. int i;
    5. QString name;
    6. };
    7. }
    8.  
    9. Q_DECLARE_METATYPE(Ui::MyStruct)
    10. #endif // DIALOG_H
    To copy to clipboard, switch view to plain text mode 

    .cpp file
    Qt Code:
    1. Dialog::Dialog(QWidget *parent) :
    2. QDialog(parent),
    3. ui(new Ui::Dialog)
    4. {
    5. ui->setupUi(this);
    6. test();
    7. }
    8.  
    9. void Dialog::test() {
    10. using namespace Ui;
    11. MyStruct test;
    12.  
    13. test.i=12;
    14. test.name="Hello world";
    15.  
    16. QVariant v = QVariant::fromValue(test);
    17. QQmlEngine engine;
    18. QQmlComponent component(&engine, "/home/user/projects-qt5/test_qml/MyItem.qml");
    19. QObject *object = component.create();
    20.  
    21. QVariant returnedValue;
    22. QVariant msg = "Hello from C++";
    23. QMetaObject::invokeMethod(object, "myQmlFunction",
    24. Q_RETURN_ARG(QVariant, returnedValue),
    25. Q_ARG(QVariant,v));
    26.  
    27. qDebug() << "QML function returned:" << returnedValue.toString();
    28. qDebug() << "comp error is " << component.errors();
    29. delete object;
    30.  
    31. }
    To copy to clipboard, switch view to plain text mode 
    .qml
    Qt Code:
    1. import QtQuick 2.0
    2.  
    3. // MyItem.qml
    4.  
    5. Item {
    6. function myQmlFunction(v) {
    7. console.log("Got message:",v.name)
    8. return "some return value"
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 20th April 2015 at 10:03. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Need help passing data to qml

    It doesn't work like that. Wrapping a structure into a variant will not teach Qt to understand its structure and thus be able to access its members. For that you should derive your class from QObject and make members into properties or you should use QVariantMap and manually copy each field value to the map.
    Last edited by wysota; 20th April 2015 at 10:08.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    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: Need help passing data to qml

    Quote Originally Posted by brasscs View Post
    Note the code seems to work if i pass it just a string but not with a simple struct.
    You can pass any type that is wrapable in a QVariant.

    If you mean that you can't access the data inside the variant, then see wysota's comment.

    Cheers,
    _

  4. #4
    Join Date
    Dec 2014
    Posts
    7
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Android

    Default Re: Need help passing data to qml

    ok so how about an example of passing a struct. gave up finding something online

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Need help passing data to qml

    Quote Originally Posted by brasscs View Post
    ok so how about an example of passing a struct. gave up finding something online
    Qt Code:
    1. MyStruct s;
    2. QVariantMap map;
    3. map["i"] = s.i;
    4. map["name"] = s.name;
    5. QVariant v = map;
    6. QMetaObject::invokeMethod(object, "myQmlFunction",
    7. Q_RETURN_ARG(QVariant, returnedValue),
    8. Q_ARG(QVariant,v));
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Dec 2014
    Posts
    7
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Android

    Default Re: Need help passing data to qml

    hey thanks for the code but it still does not work. this is what i am using

    .cpp
    Qt Code:
    1. using namespace Ui;
    2. QQmlEngine engine;
    3. QQmlComponent component(&engine, "/home/user/projects-qt5/test_qml/MyItem.qml");
    4. QObject *object = component.create();
    5. QVariant returnedValue;
    6.  
    7. MyStruct s;
    8. s.i=12;
    9. s.name="Hello QML";
    10. QVariantMap map;
    11. map["i"] = s.i;
    12. map["name"] = s.name;
    13.  
    14. QVariant v = map;
    15. QMetaObject::invokeMethod(object, "myQmlFunction",
    16. Q_RETURN_ARG(QVariant, returnedValue),
    17. Q_ARG(QVariant,v));
    To copy to clipboard, switch view to plain text mode 
    .h
    Qt Code:
    1. namespace Ui {
    2. class Dialog;
    3. struct MyStruct {
    4. int i;
    5. QString name;
    6. };
    7. }
    8. ...
    9. Q_DECLARE_METATYPE(Ui::MyStruct)
    10. #endif // DIALOG_H
    To copy to clipboard, switch view to plain text mode 
    .qml
    Qt Code:
    1. import QtQuick 2.0
    2.  
    3. // MyItem.qml
    4.  
    5. Item {
    6.  
    7. function myQmlFunction(v) {
    8. console.log("Got message:",v.name)
    9. return "some return value"
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 
    app output
    qml: QML Got message: undefined
    QML function returned: "some return value"
    comp error is ()
    Last edited by anda_skoa; 22nd April 2015 at 15:19. Reason: missing [code] tags

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Need help passing data to qml

    It works just fine for me.

    Qt Code:
    1. #include <QtGui>
    2. #include <QtQml>
    3.  
    4. int main(int argc, char **argv) {
    5. QGuiApplication app(argc, argv);
    6. QQmlEngine e;
    7. QQmlComponent c(&e, "test.qml");
    8. QObject *o = c.create();
    9. QVariantMap m;
    10. m["i"] = 10;
    11. m["name"] = "someName";
    12. QVariant v = m;
    13. QVariant ret;
    14. QMetaObject::invokeMethod(o, "testFunc", Q_RETURN_ARG(QVariant, ret), Q_ARG(QVariant, v));
    15. // return app.exec();
    16. return 0;
    17. }
    To copy to clipboard, switch view to plain text mode 

    javascript Code:
    1. import QtQml 2.0
    2.  
    3. QtObject {
    4. function testFunc(v) { console.log("v.name = ", v.name); }
    5. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Dec 2014
    Posts
    7
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Android

    Default Re: Need help passing data to qml

    hey wysota

    i finally found my error. the code you supplied is now working fine.

    thanks for your help.

Similar Threads

  1. passing data from forms
    By shreeram in forum Qt Programming
    Replies: 3
    Last Post: 17th March 2015, 15:37
  2. Passing data between QWizard and QWizardpages
    By Srstka in forum Qt Programming
    Replies: 1
    Last Post: 9th November 2014, 20:23
  3. Passing data to a QThread
    By lynchkp in forum Newbie
    Replies: 4
    Last Post: 10th March 2011, 22:02
  4. Passing data via signal/slot
    By gib in forum Qt Programming
    Replies: 4
    Last Post: 1st November 2010, 05:49
  5. passing data when closing a dialog
    By jimiq in forum Newbie
    Replies: 4
    Last Post: 4th November 2009, 16:48

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
  •  
Qt is a trademark of The Qt Company.