MyQuickItem gets loaded twice on accessing its object in main.cpp
-
Hi Guys: I have a
QtAppwhich many quick items in qml files. One of quick items isMyQuickItemwhich is embedded inqml/pages/SomePage.qml.MYQuickItemhas a function I need to set some data into & I am trying to get a valid instance toMyQuickItem. I get a instance by doing the following in mymain.cppbut the problem is that the constructor ofMyQuickItemgets called twice becauseview.setSource&engine.loadboth called inmain.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
MyQuickItemwithout creating it anew & thus avoid MyQuickItem's constructor getting called twice? -
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") -
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 :)