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. Sending events from Qt C++ to QML
Forum Updated to NodeBB v4.3 + New Features

Sending events from Qt C++ to QML

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 4 Posters 5.1k 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.
  • S Offline
    S Offline
    strekazoid
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      shifty
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • S Offline
        S Offline
        strekazoid
        wrote on last edited by
        #3

        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: {}

        1 Reply Last reply
        0
        • R Offline
          R Offline
          rishygupta
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mlong
            wrote on last edited by
            #5

            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.

            Software Engineer
            My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

            1 Reply Last reply
            0

            • Login

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