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 from QQuickItem 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 has MyQuickItem.qml in it. This library has access to the global QQmlApplicationEngine 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 other QQuickItems in app's main.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")) to myItem pointer.
    Secondly, doing the above does not seem to load MyQuickItem correctly as I don't get a call to updatePaintNode which I have overridden. I need the Qt/QML window system to call my MyQuickItem ::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 other QQuickItems?



  • 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 a qmlRegisterType<MyQuickItem>("MyQuickItem", 1, 0, "MyQuickItem"). But that does not help. I think this only works if I was doing an import MyQuickItem 1.0 to embed MyQuickItem into the Application's main.qml or any of its child. Here MyQuickItem.qml is inside a library. So struggling to make it visible under the application's qml tree


  • Lifetime Qt Champion

    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 define MyQuickItem in main.qml & call qmlRegisterType from main.cpp. All I want to do is, move the MyQuickItem into its own MyQuickItem.qml inside my library without the app defining it. The goal is that the app should get MyQuickItem 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?


  • Lifetime Qt Champion

    Then isn't the Creating C++ Plugins for QML chapter what you are looking for ?



  • @SGaist thanks. Marked this as solved now


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.