Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Calling QML functions from C++
Forum Updated to NodeBB v4.3 + New Features

Calling QML functions from C++

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 734 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    neda
    wrote on last edited by
    #1

    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.

    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    
        QObject * object = engine.rootObjects().value(0);
        
    	QVariant returnedValue;
    	QVariant msg = "Hello from C++";
        QMetaObject::invokeMethod(object, "myQmlFunction",
                Q_RETURN_ARG(QVariant, returnedValue),
                Q_ARG(QVariant, msg));
    
        return app.exec();
    }
    

    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 , ....) on the QML side from C++.
    I have this code in "MySerialPort" class:

    void MySerialPort::readData()
    {
    	QByteArray data = serial->readAll();
    	if(data[0]=='0') // For example
    		Show qml dialog
    	if(data[1]=='1') // For example
    	   disable GroupBox	
    
    ...
    ...
    }
    

    Is there any solution to handling this code on the QML side?

    1 Reply Last reply
    0
    • X Offline
      X Offline
      xargs1
      wrote on last edited by
      #2

      From the C++ code, emit a signal based on what you've read from the serial port. The QML code catches the signals and does whatever is appropriate.

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved