Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. MyQuickItem gets loaded twice on accessing its object in main.cpp
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved Mobile and Embedded
4 Posts 2 Posters 929 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
    Nelson_Piquet
    wrote on last edited by
    #1

    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
    0
    • N Offline
      N Offline
      Nelson_Piquet
      wrote on last edited by Nelson_Piquet
      #2

      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
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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
        0
        • N Offline
          N Offline
          Nelson_Piquet
          wrote on last edited by
          #4

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

          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