Solved 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
MyQuickItem
class derived fromQQuickItem
as 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
qrc
which hasMyQuickItem.qml
in it. This library has access to the globalQQmlApplicationEngine
object of the app as well.My question: How can I load
MyQuickItem
from inside my library so that it gets registered with QML like the otherQQuickItem
s 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"))
tomyItem
pointer.
Secondly, doing the above does not seem to loadMyQuickItem
correctly as I don't get a call toupdatePaintNode
which I have overridden. I need theQt/QML
window system to call myMyQuickItem ::updatePaintNode
as I have important logic there.So, How can I correctly load
MyQuickItem
from inside my library so that it gets registered & updated like otherQQuickItem
s? -
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.0
to embedMyQuickItem
into the Application'smain.qml
or any of its child. HereMyQuickItem.qml
is 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 ?
-
@SGaist I saw this example. but that one is not deriving the class from a
QQuickItem
. In my case everything is working if I defineMyQuickItem
inmain.qml
& callqmlRegisterType
from main.cpp. All I want to do is, move theMyQuickItem
into its ownMyQuickItem.qml
inside my library without the app defining it. The goal is that the app should getMyQuickItem
from the library.Isn't this possible without going into the QML module technique?
-
By the way, I can create a
QQuickitem
in 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_item
to the children list of qml objects? -
Then isn't the Creating C++ Plugins for QML chapter what you are looking for ?
-
@SGaist thanks. Marked this as solved now