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. [solved]How can I register C++ class to QML?
Forum Updated to NodeBB v4.3 + New Features

[solved]How can I register C++ class to QML?

Scheduled Pinned Locked Moved QML and Qt Quick
c++qml
12 Posts 3 Posters 4.4k 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.
  • B Offline
    B Offline
    beidaochuan
    wrote on last edited by beidaochuan
    #1

    I want to add just one command
    [qmlRegisterType<QmlConnector>("MyLib", 1, 0, "QmlConnector");]
    to "main.cpp" for registering C++ to QML.
    Class QMLConnector is used to manager(get) another classes object.
    But [QMetaProperty::read: Unable to handle unregistered datatype for property] error happend.
    how should i deal with it?
    please help me ,thanks.

    p3c0P V 2 Replies Last reply
    0
    • B beidaochuan

      I want to add just one command
      [qmlRegisterType<QmlConnector>("MyLib", 1, 0, "QmlConnector");]
      to "main.cpp" for registering C++ to QML.
      Class QMLConnector is used to manager(get) another classes object.
      But [QMetaProperty::read: Unable to handle unregistered datatype for property] error happend.
      how should i deal with it?
      please help me ,thanks.

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

      Hi @beidaochuan Is that complete error ? Did it print which datatype and which property ?

      157

      B 1 Reply Last reply
      0
      • p3c0P p3c0

        Hi @beidaochuan Is that complete error ? Did it print which datatype and which property ?

        B Offline
        B Offline
        beidaochuan
        wrote on last edited by
        #3

        @p3c0
        I want to make the source form ① to ② as follows.
        ①
        int main(int argc, char *argv[]){
        QApplication app(argc, argv);
        qmlRegisterType<A>("MyLib", 1, 0, "A");
        qmlRegisterType<B>("MyLib", 1, 0, "B");
        qmlRegisterType<C>("MyLib", 1, 0, "C");
        QQmlApplicationEngine engine;
        engine.load(QUrl(QStringLiteral("qrc:/form/main.qml")));
        return app.exec();
        }
        ②
        int main(int argc, char *argv[]){
        QApplication app(argc, argv);
        qmlRegisterType<ALL>("MyLib", 1, 0, "ALL");
        QQmlApplicationEngine engine;
        engine.load(QUrl(QStringLiteral("qrc:/form/main.qml")));
        return app.exec();
        }

        class ALL is used to get objects of class A, B ,C.

        p3c0P 1 Reply Last reply
        0
        • B beidaochuan

          @p3c0
          I want to make the source form ① to ② as follows.
          ①
          int main(int argc, char *argv[]){
          QApplication app(argc, argv);
          qmlRegisterType<A>("MyLib", 1, 0, "A");
          qmlRegisterType<B>("MyLib", 1, 0, "B");
          qmlRegisterType<C>("MyLib", 1, 0, "C");
          QQmlApplicationEngine engine;
          engine.load(QUrl(QStringLiteral("qrc:/form/main.qml")));
          return app.exec();
          }
          ②
          int main(int argc, char *argv[]){
          QApplication app(argc, argv);
          qmlRegisterType<ALL>("MyLib", 1, 0, "ALL");
          QQmlApplicationEngine engine;
          engine.load(QUrl(QStringLiteral("qrc:/form/main.qml")));
          return app.exec();
          }

          class ALL is used to get objects of class A, B ,C.

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

          @beidaochuan Ok. Not sure. But what approach did you follow to get that error you posted earlier ?

          157

          B 2 Replies Last reply
          0
          • p3c0P p3c0

            @beidaochuan Ok. Not sure. But what approach did you follow to get that error you posted earlier ?

            B Offline
            B Offline
            beidaochuan
            wrote on last edited by p3c0
            #5

            @p3c0
            I just modified the class ALL as follows.

            // .h
            class ALL : public QObject
            {
                Q_OBJECT
            public:
                explicit ALL(QObject *parent = 0);
            
                Q_PROPERTY(A* bmA READ bmA NOTIFY bmAChanged)
                Q_INVOKABLE A *bmA();
            
                Q_PROPERTY(B* bmB READ bmB NOTIFY bmBChanged)
                Q_INVOKABLE B *bmB();
            
            Q_PROPERTY(C* bmC READ bmC NOTIFY bmCChanged)
                Q_INVOKABLE C *bmA();
                
            private:
                A *a;
                B * b;
                C *c;
            
            signals:
                void bmAChanged();
                void bmBChanged();
                void bmCChanged();
            
            public slots:
            };
            
            //.cpp
            
            ALL::ALL(QObject *parent) : QObject(parent)
            {
                a = new A(this);
                b = new B(this);
                c = new C(this);
            }
            
            A *ALL::bmA()
            {
                return a;
            }
            
            B *ALL::bmB()
            {
                return b;
            }
            
            C *ALL::bmC()
            {
                return c;
            }
            
            1 Reply Last reply
            0
            • p3c0P p3c0

              @beidaochuan Ok. Not sure. But what approach did you follow to get that error you posted earlier ?

              B Offline
              B Offline
              beidaochuan
              wrote on last edited by
              #6

              @p3c0
              when I ran the project, the errors happend.

              QMetaProperty::read: Unable to handle unregistered datatype 'A*' for property 'ALL::bmA'
              QMetaProperty::read: Unable to handle unregistered datatype 'B*' for property 'ALL::bmB'
              QMetaProperty::read: Unable to handle unregistered datatype 'C*' for property 'ALL::bmC'

              p3c0P 1 Reply Last reply
              0
              • B beidaochuan

                @p3c0
                when I ran the project, the errors happend.

                QMetaProperty::read: Unable to handle unregistered datatype 'A*' for property 'ALL::bmA'
                QMetaProperty::read: Unable to handle unregistered datatype 'B*' for property 'ALL::bmB'
                QMetaProperty::read: Unable to handle unregistered datatype 'C*' for property 'ALL::bmC'

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

                @beidaochuan To make pointers available in Q_PROPERTY you will need to register it first as:

                qRegisterMetaType<A*>("A*");
                

                Then since it is declared as a property no need to make the accessor Q_INVOKABLE. So

                //In cpp
                Q_PROPERTY(A* bmA READ bmA NOTIFY bmAChanged)
                
                A *MyClass::bmA() const
                {
                    return a;
                }
                
                private:
                   A* a;
                     
                //In QML
                //Access it as usual property
                All {
                    id: all
                }
                
                console.log(all.bmA) //should get A object.
                

                157

                B 1 Reply Last reply
                0
                • p3c0P p3c0

                  @beidaochuan To make pointers available in Q_PROPERTY you will need to register it first as:

                  qRegisterMetaType<A*>("A*");
                  

                  Then since it is declared as a property no need to make the accessor Q_INVOKABLE. So

                  //In cpp
                  Q_PROPERTY(A* bmA READ bmA NOTIFY bmAChanged)
                  
                  A *MyClass::bmA() const
                  {
                      return a;
                  }
                  
                  private:
                     A* a;
                       
                  //In QML
                  //Access it as usual property
                  All {
                      id: all
                  }
                  
                  console.log(all.bmA) //should get A object.
                  
                  B Offline
                  B Offline
                  beidaochuan
                  wrote on last edited by
                  #8

                  @p3c0
                  Thanks a lot.
                  That means I must register class A,B,C and class ALL to QML, and just manager A,B,C with class ALL.
                  In QML I can use like this:
                  ALL {id: all}
                  all.bmA
                  all.bmB
                  all.bmC

                  that is right?

                  p3c0P 1 Reply Last reply
                  0
                  • B beidaochuan

                    @p3c0
                    Thanks a lot.
                    That means I must register class A,B,C and class ALL to QML, and just manager A,B,C with class ALL.
                    In QML I can use like this:
                    ALL {id: all}
                    all.bmA
                    all.bmB
                    all.bmC

                    that is right?

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

                    @beidaochuan Class A,B and C as:

                    qRegisterMetaType<A*>("A*");
                    qRegisterMetaType<B*>("B*");
                    

                    to make pointers available
                    And class ALL as usual using qmlRegisterType

                    qmlRegisterType<ALL>("ALL", 1, 0, "ALL");
                    

                    In QML I can use like this:
                    ALL {id: all}
                    all.bmA
                    all.bmB
                    all.bmC

                    that is right?

                    Right.

                    157

                    B 1 Reply Last reply
                    1
                    • p3c0P p3c0

                      @beidaochuan Class A,B and C as:

                      qRegisterMetaType<A*>("A*");
                      qRegisterMetaType<B*>("B*");
                      

                      to make pointers available
                      And class ALL as usual using qmlRegisterType

                      qmlRegisterType<ALL>("ALL", 1, 0, "ALL");
                      

                      In QML I can use like this:
                      ALL {id: all}
                      all.bmA
                      all.bmB
                      all.bmC

                      that is right?

                      Right.

                      B Offline
                      B Offline
                      beidaochuan
                      wrote on last edited by
                      #10

                      @p3c0
                      It's very kind of you.
                      Thank you very much.

                      p3c0P 1 Reply Last reply
                      0
                      • B beidaochuan

                        @p3c0
                        It's very kind of you.
                        Thank you very much.

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

                        @beidaochuan Glad that I was helpful :)
                        Please mark the post as [Solved] if done.

                        157

                        1 Reply Last reply
                        0
                        • B beidaochuan

                          I want to add just one command
                          [qmlRegisterType<QmlConnector>("MyLib", 1, 0, "QmlConnector");]
                          to "main.cpp" for registering C++ to QML.
                          Class QMLConnector is used to manager(get) another classes object.
                          But [QMetaProperty::read: Unable to handle unregistered datatype for property] error happend.
                          how should i deal with it?
                          please help me ,thanks.

                          V Offline
                          V Offline
                          vladstelmahovsky
                          wrote on last edited by
                          #12

                          @beidaochuan take a look here https://forum.qt.io/topic/54576/wrote-short-article-about-dynamically-created-qquickitem-s-with-example-code

                          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