Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    Solved MyQuickItem gets loaded twice on accessing its object in main.cpp

    Mobile and Embedded
    2
    4
    644
    Loading More Posts
    • 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
      Nelson_Piquet last edited by

      Hi Guys: I have a QtApp which many quick items in qml files. One of quick items is MyQuickItem which is embedded in qml/pages/SomePage.qml. MYQuickItem has a function I need to set some data into & I am trying to get a valid instance to MyQuickItem. I get a instance by doing the following in my main.cpp but the problem is that the constructor of MyQuickItem gets called twice because view.setSource & engine.load both called in main.cpp.

      My main.cpp

      int main(int argc, char *argv[]) 
      {
        QQmlApplicationEngine engine;
        qmlRegisterType<MyQuickItem>("MyQuickItem", 1, 0, "MyQuickItem");
        QQuickView view;
        //Mainpage.qml is where MyQuickItem is located. Its sort of a child page of main.qml 
        view.setSource(QUrl(QStringLiteral("qrc:/qml/pages/MainPage.qml")));
        QObject *object = view.rootObject();
        QObject *rect = object->findChild<QObject*>("myQuickItem"); 
        MyQuickItem* quickItem = qobject_cast<MyQuickItem*>(rect);
        quickItem->SetCppObject(theCppObject);
        engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
        return app.exec();
      }
      

      My MainPage.qml

      import MyQuickItem 1.0
      
      MyQuickItem {
        id: myQuickItemID
        visible: true
        objectName: "myQuickItem"
      }
      

      MyQuickItem.h

      class MyQuickItem : public QQuickItem {
        MyQuickItem();
        SetCppObject(MyCppLibApiClass cppObject);
        MyCppLibApiClass m_cppObject;
      }
      

      Can you please suggest how I can get a valid instance to MyQuickItem without creating it anew & thus avoid MyQuickItem's constructor getting called twice?

      1 Reply Last reply Reply Quote 0
      • N
        Nelson_Piquet last edited by Nelson_Piquet

        I got this working by doing the followind way of acquiring the quickItem object very much after help from this forum in my previous questions.

        QQuickItem *myItem = engine.rootObjects()[0]->findChild<QQuickItem *>("myQuickItem")

        1 Reply Last reply Reply Quote 0
        • SGaist
          SGaist Lifetime Qt Champion last edited by

          Hi,

          Since you solved the problem, please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :)

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply Reply Quote 0
          • N
            Nelson_Piquet last edited by

            surely yes. @p3c0 @SGaist thanks for the support guys

            1 Reply Last reply Reply Quote 0
            • First post
              Last post