Sending events from Qt C++ to QML



  • After spending almost the whole day trying to figure out this I decided to post a new topic :D

    Ok, here is a deal. I have an event on Qt C++ side, and I want to propagate it to QML UI. What is the proper way of doing this?

    I have QML application with some engine logic on C++ side. QML app is made of QApplication and QmlApplicationViewer (generated by the project wizard):

    @QScopedPointer<QApplication> app(createApplication(argc, argv));
    QScopedPointer<QmlApplicationViewer> viewer(QmlApplicationViewer::create());@

    One approach I found was this: http://stackoverflow.com/questions/5709820/communication-between-c-and-qml
    But it doesn't work for me. This is what I tried:

    @Q_DECL_EXPORT int main(int argc, char *argv[])
    {
    QScopedPointer<QApplication> app(createApplication(argc, argv));
    QScopedPointer<QmlApplicationViewer> viewer(QmlApplicationViewer::create());

    QObject *rootContext = viewer->rootContext();
    
    PropertyListener* listener = new PropertyListener(rootContext);
    viewer->rootContext()->setContextProperty("PropertyListener", listener);
    
    // Finally launch the UI
    viewer->setMainQmlFile&#40;QLatin1String("qml/RingiCall/main.qml"&#41;);
    viewer->showExpanded();
    
    // Some test code
    QObject *myText = viewer->rootContext()->findChild<QObject *>(QString("nameText"));
       if (myText) { // element found
           myText->setProperty("text", QVariant(QString("Hello")));
       } else {
           qDebug() << "'myText' not found";
       }
    
    return app->exec&#40;&#41;;
    

    }@

    I also have MainPage.qml with a Text element and objectName property set to "nameText". The thing is that findChild is unable to find anything within the declarative context. Should it even work like that or am I missing something?



  • Hi, I'm pretty new at this myself, but I found this page to be pretty useful for information about C++ and QML together: "Using QML Bindings in C++ Applications":http://doc.qt.nokia.com/4.7-snapshot/qtbinding.html#loading-qml-components-from-c

    Hope this helps you



  • Thanks, shifty. I solved this now in a completely different manner, using this example:
    http://doc.qt.nokia.com/latest/declarative-cppextensions-referenceexamples-signal.html

    Here is also a nice article about events: http://doc.qt.nokia.com/latest/qmlevents.html

    I've created now my own custom class, declared properties as QML properties, registered my class as a new type in QML, and finally created an object of my class in QML. After that I'm able to propagate signal from C++ to QML. It is all done pretty much according to example under first link.

    In case anyone trying this, watch out for your signal method name. It should start with a small letter (not capital!). Like - void superSignal(const QString &json);

    After that you'll be able to catch it in QML using onSuperSignal: {}



  • can we use or manipulate QmlApplicationViewer in any other cpp(class) rather than main?


  • Moderators

    rishygupta, you have asked this question in a number of threads, a couple of which are very old threads. Please open a separate thread for a new question and please don't duplicate your questions. Thanks.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.