How to load a custom QQuickItem from inside a library so that it gets registered & updated like other QQuickItems in the application
-
I have a
MyQuickItemclass derived fromQQuickItemas below// MyQuickItem.hpp class MyQuickItem : public QQuickItem { Q_OBJECT public: MyQuickItem(); virtual ~ MyQuickItem(); protected: QSGNode* updatePaintNode(QSGNode *, UpdatePaintNodeData *) override; };Following is
MyQuickItem.qml.import QtQuick 2.0 import MyQuickItem 1.0 Item { MyQuickItem { id: my_quick_item objectName: "MyQuickItemObject" visible: false } }Point to be noted is that all of above in a separate static library. And the library has a
qrcwhich hasMyQuickItem.qmlin it. This library has access to the globalQQmlApplicationEngineobject of the app as well.My question: How can I load
MyQuickItemfrom inside my library so that it gets registered with QML like the otherQQuickItems in app'smain.qml?I am trying something around the following way from inside my library in a C++ method called after main.qml is loaded by the application:
MyQuickItem * myItem = new MyQuickItem(); myItem->setParent(m_qml_engine->parent()); myItem->setParentItem(qobject_cast<QQuickItem*>(m_qml_engine->parent())); QQmlEngine::setObjectOwnership(myItem, QQmlEngine::JavaScriptOwnership); myItem->setHeight(500); // But myItem is NULL here !!! myItem->setHeight(500); // But myItem is NULL here !!!Firstly, I don't know how to link
QUrl(QStringLiteral("qrc:/qml/MyQuickItem.qml"))tomyItempointer.
Secondly, doing the above does not seem to loadMyQuickItemcorrectly as I don't get a call toupdatePaintNodewhich I have overridden. I need theQt/QMLwindow system to call myMyQuickItem ::updatePaintNodeas I have important logic there.So, How can I correctly load
MyQuickItemfrom inside my library so that it gets registered & updated like otherQQuickItems? -
Hi,
just a wild guess since I am a noob. Have you tried to include the header file from your lib in your main.cpp? If not try that and look up qmlRegisterType.
-
Hi,
just a wild guess since I am a noob. Have you tried to include the header file from your lib in your main.cpp? If not try that and look up qmlRegisterType.
@Sikarjan I tried including
MyLibrary/MyQuickItem.hpp/ do aqmlRegisterType<MyQuickItem>("MyQuickItem", 1, 0, "MyQuickItem"). But that does not help. I think this only works if I was doing animport MyQuickItem 1.0to embedMyQuickIteminto the Application'smain.qmlor any of its child. HereMyQuickItem.qmlis inside a library. So struggling to make it visible under the application's qml tree -
Hi and welcome to devnet,
Did you already saw the QML Modules chapter in Qt's documentation ?
-
Hi and welcome to devnet,
Did you already saw the QML Modules chapter in Qt's documentation ?
@SGaist I saw this example. but that one is not deriving the class from a
QQuickItem. In my case everything is working if I defineMyQuickIteminmain.qml& callqmlRegisterTypefrom main.cpp. All I want to do is, move theMyQuickIteminto its ownMyQuickItem.qmlinside my library without the app defining it. The goal is that the app should getMyQuickItemfrom the library.Isn't this possible without going into the QML module technique?
-
By the way, I can create a
QQuickitemin the following way.QQuickItem * dynamic_quick_item = new QQuickItem(); dynamic_quick_item->setObjectName("DynamicQuickItemObject"); dynamic_quick_item->setHeight(500); dynamic_quick_item->setWidth(500);I also have access to qml_engine & everything in
main.cpp.But my problem is that : How can I add this
dynamic_quick_itemto the children list of qml objects? -
Then isn't the Creating C++ Plugins for QML chapter what you are looking for ?
-
Then isn't the Creating C++ Plugins for QML chapter what you are looking for ?
@SGaist thanks. Marked this as solved now