Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to set a pure C++ object instance in a QQuickItem
Forum Updated to NodeBB v4.3 + New Features

How to set a pure C++ object instance in a QQuickItem

Scheduled Pinned Locked Moved Solved Mobile and Embedded
16 Posts 3 Posters 4.1k Views 2 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.
  • p3c0P p3c0

    @Nelson_Piquet You can also instantiate the MyCppLibApiClass in main.cpp then set it as a context property which will then be accessible from MyQuickItem in QML.
    Something like

    qmlRegisterType<MyQuickItem>("MyQuickItem", 1, 0, "MyQuickItem");
    
    MyCppLibApiClass myClass;
    
    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
    engine.rootContext()->setContextProperty("myObj", &myClass); // Access myObj from QML
    
    return app.exec();
    

    More info here:
    http://doc.qt.io/qt-5/qtqml-cppintegration-contextproperties.html

    N Offline
    N Offline
    Nelson_Piquet
    wrote on last edited by Nelson_Piquet
    #4

    @p3c0 @SGaist Thanks a lot for your attention. p3c0's answer suggests how to make MyCppLibApiClass recognisable on qml side. but, I cannot take this approach because this requires to make MyCppLibApiClass inherited by QObject if I am correct. MyCppLibApiClass comes from a pure C++ library & I cannot afford to introduce Qt into it. But My primary question is slightly different here. My question is: How to get a valid instance of MyQuickItem & call SetCppObject which accepts a pure Cpp obejct i.e. MyCppLibApiClass ? I have updated my question & sample code in the question to make it clearer

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #5

      @Nelson_Piquet So as said earlier in the last post use findChild to get access to QML instance on C++ side. Now once you get this object call the SetCppObject function and pass cppObject to it.

      157

      N 1 Reply Last reply
      1
      • p3c0P p3c0

        @Nelson_Piquet So as said earlier in the last post use findChild to get access to QML instance on C++ side. Now once you get this object call the SetCppObject function and pass cppObject to it.

        N Offline
        N Offline
        Nelson_Piquet
        wrote on last edited by Nelson_Piquet
        #6

        @p3c0 I found that just doing a findChild does not give me access to SetCppObject. Looks like I have to cast it to MyQuickItem to get access to SetCppObject. Following compiles:

        QQuickView view;
        view.setSource(QUrl::fromLocalFile("main.qml"));
        QObject *object = view.rootObject();
        QObject *rect = object->findChild<QObject*>("MyQuickItem");
        MyQuickItem* quickItemObj = qobject_cast<MyQuickItem*>(rect);
        quickItemObj->SetCppObject(theCppObject);
        

        But qobject_cast returns me a null pointer. Q_OBJECT macro is included in MyQuickItem

        class MyQuickItem : public QQuickItem {
          Q_OBJECT
        public:
          MyQuickItem();
        }
        

        Should I do something in MyQuickItem class to get a valid pointer to MyQuickItem ? Or pls correct me if I am going wrong here

        1 Reply Last reply
        0
        • p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by p3c0
          #7

          @Nelson_Piquet The object name that findChild accepts is case sensitive. I see a mismatch between this and your first post. ie. myQuickItem and MyQuickItem are totally different.

          But qobject_cast returns me a null pointer.

          Probably because the object was not found at first.

          157

          N 1 Reply Last reply
          0
          • p3c0P p3c0

            @Nelson_Piquet The object name that findChild accepts is case sensitive. I see a mismatch between this and your first post. ie. myQuickItem and MyQuickItem are totally different.

            But qobject_cast returns me a null pointer.

            Probably because the object was not found at first.

            N Offline
            N Offline
            Nelson_Piquet
            wrote on last edited by Nelson_Piquet
            #8

            @p3c0 Thanks for point that out. I corrected the id. Also, the main.qml path I inputted was not the full path. It needs to be like so: qrc:/qml/main.qml. After applying a qobject_cast & I get a pointer to MyQuickItem.

            But when I try to access SetCppObject, it crashes reporting an error which says : QQuickView does not support using windows as a root item. If you wish to create your root window from QML, consider using QQmlApplicationEngine instead. But its a separate issue. I will read a little & put a separate question for that. Thanks a lot for helping until here.

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

              Maybe a silly question but do you really need a QQuickItem based wrapper ? Wouldn't a simple QObject wrapper do the work ?

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

              N 1 Reply Last reply
              1
              • N Nelson_Piquet

                @p3c0 Thanks for point that out. I corrected the id. Also, the main.qml path I inputted was not the full path. It needs to be like so: qrc:/qml/main.qml. After applying a qobject_cast & I get a pointer to MyQuickItem.

                But when I try to access SetCppObject, it crashes reporting an error which says : QQuickView does not support using windows as a root item. If you wish to create your root window from QML, consider using QQmlApplicationEngine instead. But its a separate issue. I will read a little & put a separate question for that. Thanks a lot for helping until here.

                p3c0P Offline
                p3c0P Offline
                p3c0
                Moderators
                wrote on last edited by
                #10

                @Nelson_Piquet As the error says QQuickView can only load root element of QML types that inherits Item whereas QQmlApplicationEngine loads root elements Window and ApplicationWindow.

                157

                1 Reply Last reply
                0
                • SGaistS SGaist

                  Maybe a silly question but do you really need a QQuickItem based wrapper ? Wouldn't a simple QObject wrapper do the work ?

                  N Offline
                  N Offline
                  Nelson_Piquet
                  wrote on last edited by
                  #11

                  @SGaist I really need a QQuickItem based wrapper. it is embedded in qml. I am not sure of how to make it work as a QObject wrapper. but, now I've reached a point with all the help where I am able to retrieve the object of MyQuickItem. But now, MyQuickItem's constructor gets called twice which is not what I need.......aahh !! Following is 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"); //pls note that myQuickItem is the object name
                    MyQuickItem* quickItem = qobject_cast<MyQuickItem*>(rect);
                    quickItem->SetCppObject(theCppObject);
                    engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
                    return app.exec();
                  }
                  

                  My doubt is that view.setSource might need to be avoided but not sure how

                  1 Reply Last reply
                  0
                  • p3c0P Offline
                    p3c0P Offline
                    p3c0
                    Moderators
                    wrote on last edited by
                    #12

                    @Nelson_Piquet There is no need to load 2 QML files separetely. This is going to create 2 windows and thus complicate. Just load your main QML using QQuickView or QQmlApplicationEngine and from there load other QML files.

                    157

                    N 1 Reply Last reply
                    0
                    • p3c0P p3c0

                      @Nelson_Piquet There is no need to load 2 QML files separetely. This is going to create 2 windows and thus complicate. Just load your main QML using QQuickView or QQmlApplicationEngine and from there load other QML files.

                      N Offline
                      N Offline
                      Nelson_Piquet
                      wrote on last edited by
                      #13

                      @p3c0 What should I change in my main.cpp to avoid loading the files twice ? If I dont call view.setSource, I will not get hold of the child object. If dont call engine.load, I cannot load my main.qml. How should I change my main.cpp. Since this question is a separate issue, I have posted a new question in this forum here

                      1 Reply Last reply
                      0
                      • N Offline
                        N Offline
                        Nelson_Piquet
                        wrote on last edited by
                        #14

                        @p3c0 @SGaist No need to answer the other question I posted. I got it working. Thanks a lot guys. Thanks a many !!!

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

                          About the QObject wrapper, unless I misunderstood, you only want to act on your MyCppLibApiClass and not paint anything related to it thus the QObject wrapper can be used without any dependency on a visual element.

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

                          N 1 Reply Last reply
                          0
                          • SGaistS SGaist

                            About the QObject wrapper, unless I misunderstood, you only want to act on your MyCppLibApiClass and not paint anything related to it thus the QObject wrapper can be used without any dependency on a visual element.

                            N Offline
                            N Offline
                            Nelson_Piquet
                            wrote on last edited by
                            #16

                            @SGaist understood. thanks

                            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