Results 1 to 6 of 6

Thread: Calling QML functions from C++

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2016
    Posts
    81
    Thanks
    31
    Qt products
    Qt5
    Platforms
    Windows

    Default Calling QML functions from C++

    Hi,
    I use this code in "main.cpp" and that is Ok.
    But I do not know How can I use this code in another c++ class.
    When I put this code in another class (for example myserialport.cpp), program does not work.

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4.  
    5. QQmlApplicationEngine engine;
    6. engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    7.  
    8. QObject * object = engine.rootObjects().value(0);
    9.  
    10. QVariant returnedValue;
    11. QVariant msg = "Hello from C++";
    12. QMetaObject::invokeMethod(object, "myQmlFunction",
    13. Q_RETURN_ARG(QVariant, returnedValue),
    14. Q_ARG(QVariant, msg));
    15.  
    16. return app.exec();
    17. }
    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: Calling QML functions from C++

    It is usally a bad idea to call a QML function from C++.
    Why do you want to do that?
    Do you need the functions return value?

    Cheers,
    _

  3. #3
    Join Date
    Jan 2016
    Posts
    81
    Thanks
    31
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Calling QML functions from C++

    Quote Originally Posted by anda_skoa View Post
    It is usally a bad idea to call a QML function from C++.
    Why do you want to do that?

    _
    Thank you,
    I want to change source icon of toolbutton, show dialog with specific message , change value of several property and enable or disable controls (TextField,GroupBox , ....).

    Quote Originally Posted by anda_skoa View Post
    Do you need the functions return value?

    _
    No
    Last edited by neda; 8th March 2016 at 11:19.

  4. #4
    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: Calling QML functions from C++

    That all sounds like it would be better handled on the QML side, reacting to changes of C++ data.

    But maybe you can post the function you are attempting to call?

    Because needing the return value is basically the only use case where one would even consider a C++ -> QML dependency and even that often has better alternatives.

    Cheers,
    _

  5. #5
    Join Date
    Jan 2016
    Posts
    81
    Thanks
    31
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Calling QML functions from C++

    Thank you,
    I have this code in "MySerialPort" class:
    Qt Code:
    1. void MySerialPort::readData()
    2. {
    3. QByteArray data = serial->readAll();
    4. if(data[0]=='0') // For example
    5. Show qml dialog
    6. if(data[1]=='1') // For example
    7. disable GroupBox
    8.  
    9. ...
    10. ...
    11. }
    To copy to clipboard, switch view to plain text mode 

    How can I handled this code on the QML side?

  6. #6
    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: Calling QML functions from C++

    The usual way is to have a property on a C++ object that is exposed to QML.
    I think we've already discussed this before.

    In this case maybe an int or enum property.

    QML then reacts to changes of that property the usual way, e.g. via property bindings.

    Or you emit signals and handle these in QML, e.g. using a Connection element

    Cheers,
    _

  7. The following user says thank you to anda_skoa for this useful post:

    neda (12th March 2016)

Similar Threads

  1. Replies: 2
    Last Post: 18th October 2012, 13:31
  2. Calling class functions from another function
    By prophet0 in forum General Programming
    Replies: 2
    Last Post: 2nd March 2012, 21:16
  3. ActiveQt not calling COM functions
    By daveg in forum Qt Programming
    Replies: 3
    Last Post: 11th October 2011, 09:45
  4. Replies: 5
    Last Post: 17th November 2010, 17:29
  5. Calling GUI functions from a DLL
    By inktomi in forum Qt Programming
    Replies: 0
    Last Post: 19th July 2010, 12:50

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.