Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to load a custom QQuickItem from inside a library so that it gets registered & updated like other QQuickItems in the application

How to load a custom QQuickItem from inside a library so that it gets registered & updated like other QQuickItems in the application

Scheduled Pinned Locked Moved Solved QML and Qt Quick
8 Posts 3 Posters 3.7k 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.
  • Roby BrundleR Offline
    Roby BrundleR Offline
    Roby Brundle
    wrote on last edited by Roby Brundle
    #1

    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?

    1 Reply Last reply
    0
    • SikarjanS Offline
      SikarjanS Offline
      Sikarjan
      wrote on last edited by Sikarjan
      #2

      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.

      Roby BrundleR 1 Reply Last reply
      0
      • SikarjanS Sikarjan

        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.

        Roby BrundleR Offline
        Roby BrundleR Offline
        Roby Brundle
        wrote on last edited by Roby Brundle
        #3

        @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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi and welcome to devnet,

          Did you already saw the QML Modules chapter in Qt's documentation ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          Roby BrundleR 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi and welcome to devnet,

            Did you already saw the QML Modules chapter in Qt's documentation ?

            Roby BrundleR Offline
            Roby BrundleR Offline
            Roby Brundle
            wrote on last edited by Roby Brundle
            #5

            @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?

            1 Reply Last reply
            0
            • Roby BrundleR Offline
              Roby BrundleR Offline
              Roby Brundle
              wrote on last edited by Roby Brundle
              #6

              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?

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

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

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                Roby BrundleR 1 Reply Last reply
                0
                • SGaistS SGaist

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

                  Roby BrundleR Offline
                  Roby BrundleR Offline
                  Roby Brundle
                  wrote on last edited by
                  #8

                  @SGaist thanks. Marked this as solved now

                  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