Results 1 to 8 of 8

Thread: Need help passing data to qml

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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.