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. How to execute C++ code when QML initialization is complete?
Forum Updated to NodeBB v4.3 + New Features

How to execute C++ code when QML initialization is complete?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 5 Posters 3.7k Views 3 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
    Steve Fallows
    wrote on 7 Aug 2017, 15:42 last edited by
    #1

    I am trying to connect a QML signal to a C++ slot when my application starts. I am trying to use the following snippet of code to do this:

    QQmlApplicationEngine *qqae = (QQmlApplicationEngine*)qmlEngine(this);
    QObject *item = qqae->rootObjects().first()->findChild<QObject *>("calibrationTabItem");
    if (item == nullptr)
    {
        qDebug() <<"calibrationTabItem not found";
    }
    connect(item, SIGNAL(performOffsetCalibration),
            this, SLOT(PerformOffsetCalibration));
    

    This code is executed in the Component.onCompleted of a custom type created with qmlRegisterType. The problem is that the list returned by rootObjects() is empty. It seems odd that the rootObjects list is empty during an onCompleted call.

    I have tried executing the same code in a button onClicked handler, just as a way of running it after all initialization is complete. In that case the list of rootObjects is not empty and the code works correctly.

    How can I connect this code to a function/signal that happens after all the QML code is initialized?

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on 7 Aug 2017, 15:50 last edited by A Former User 8 Jul 2017, 15:57
      #2

      Hi! completed is emited when the QML engine created the object / added it to the object tree. It doesn't say anything about the current state of its potential child objects or attached objected. I personally find the following approach for integrating C++ and QML much nicer: https://forum.qt.io/post/354757

      1 Reply Last reply
      2
      • J Offline
        J Offline
        jeremy_k
        wrote on 7 Aug 2017, 23:35 last edited by
        #3

        @Steve-Fallows said in How to execute C++ code when QML initialization is complete?:

        I am trying to connect a QML signal to a C++ slot when my application starts. I am trying to use the following snippet of code to do this:

        QQmlApplicationEngine *qqae = (QQmlApplicationEngine*)qmlEngine(this);
        QObject *item = qqae->rootObjects().first()->findChild<QObject *>("calibrationTabItem");
        if (item == nullptr)
        {
            qDebug() <<"calibrationTabItem not found";
        }
        connect(item, SIGNAL(performOffsetCalibration),
                this, SLOT(PerformOffsetCalibration));
        

        This code is executed in the Component.onCompleted of a custom type created with qmlRegisterType. The problem is that the list returned by rootObjects() is empty. It seems odd that the rootObjects list is empty during an onCompleted call.

        I have tried executing the same code in a button onClicked handler, just as a way of running it after all initialization is complete. In that case the list of rootObjects is not empty and the code works correctly.

        That's expected. Objects are added to the rootObjects list after their initialization completes. Parent objects only complete after all of their statically instantiated children. Presumably the custom object is a child of a root object, in which case the custom object must finish initialization prior to the parent.

        How can I connect this code to a function/signal that happens after all the QML code is initialized?

        The backend strategy @Wieland pointed out is one option.

        Another choice is to use the QQmlApplicationEngine::objectCreated signal and wait for the root item or calibrationTabItem to complete. The code that connects the signals should check to see if the root item is already complete, for the sake of handling dynamic object creation.

        Asking a question about code? http://eel.is/iso-c++/testcase/

        1 Reply Last reply
        0
        • P Offline
          P Offline
          Pablo J. Rogina
          wrote on 16 Aug 2017, 18:59 last edited by
          #4

          @Steve-Fallows I'd follow the approach suggested by @Wieland since it looks like a good idea, as also suggested to this question in stackoverflow

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          1
          • K Offline
            K Offline
            KillerSmath
            wrote on 16 Aug 2017, 21:50 last edited by
            #5

            Hi, could you share a snippet of code from QML file?

            @Computer Science Student - Brazil
            Web Developer and Researcher
            “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

            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