Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Exposing a user defined class / structure to qt meta object system and accessing the same in qml
QtWS25 Last Chance

Exposing a user defined class / structure to qt meta object system and accessing the same in qml

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 5 Posters 1.6k Views
  • 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.
  • D Offline
    D Offline
    divaindie
    wrote on last edited by aha_1980
    #1

    Hi all,
    i was trying something like this..i had defined my own class in cpp which is as below.

    class myclass{
    public :
    int k;int m;
    QString str;
    };
    

    and my code has one more class which has a function ' retval' which will return an object of type myclass

    class reduntclass : public QObject {
    Q_OBJECT
    public :
    ....................
    ...................
    myclass retval() {
    ...............
    ..............
    return myclass_object;
    }//ret val ends here
    ....................
    ...................
    ....................
    ...................
    };//reduntclass class ends here
    

    and i have exposed "reduntclass" object to qml (with the help of context->setContextProperty("reduntclass", &(reduntclassobj));).also iam able to access reduntclass object from qml.problem is whenever i press a key , qml will call retval function as below

    onclicked {
    ...........
    var k = reduntclass.retval()
    ...........
    }
    

    but qml is not able identify the returned value ,and it gives the below errro message
    Error: Unknown method return type: myclass
    how i can solve this? i think qml is not able to identify myclass type,so i guess i need to expose my 'myclass' class to qt meta object system?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      QML only understands QObjects. Your myclass should be a QObject, or at the very least a Q_GADGET.

      (Z(:^

      1 Reply Last reply
      3
      • D Offline
        D Offline
        divaindie
        wrote on last edited by
        #3

        if possible please give one example code

        1 Reply Last reply
        0
        • D Offline
          D Offline
          divaindie
          wrote on last edited by divaindie
          #4

          i tried the below code . in my .hpp file i have one structure and one class

          struct MyStruct {
              Q_GADGET
              int intval;
              QString str1;
              QString str2;
          };
          
          class MyClass:public QObject
              {
                  Q_OBJECT
          public:
               MyClass(QObject *parent = nullptr){
                    qDebug() <<"object created ";
              }
              MyStruct strObj;
              ~MyClass() {
              }
          MyStruct strObj;
             Q_INVOKABLE  MyStruct getMyStruct() const
               {
                   return strObj;
               }
          };
          

          in main.cpp i have below piece of code

          MyClass classObj;
              context->setContextProperty("classObj",&classObj);
          

          on qml

          onClicked: {
                       var k  = classObj.getMyStruct()
          }
          

          but still getting below error message,what is wrong with the code?? please help!!!

          Error: Unknown method return type: MyStruct
          
          jsulmJ 1 Reply Last reply
          0
          • D divaindie

            i tried the below code . in my .hpp file i have one structure and one class

            struct MyStruct {
                Q_GADGET
                int intval;
                QString str1;
                QString str2;
            };
            
            class MyClass:public QObject
                {
                    Q_OBJECT
            public:
                 MyClass(QObject *parent = nullptr){
                      qDebug() <<"object created ";
                }
                MyStruct strObj;
                ~MyClass() {
                }
            MyStruct strObj;
               Q_INVOKABLE  MyStruct getMyStruct() const
                 {
                     return strObj;
                 }
            };
            

            in main.cpp i have below piece of code

            MyClass classObj;
                context->setContextProperty("classObj",&classObj);
            

            on qml

            onClicked: {
                         var k  = classObj.getMyStruct()
            }
            

            but still getting below error message,what is wrong with the code?? please help!!!

            Error: Unknown method return type: MyStruct
            
            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              I'm not sure if Q_GADGET will work (QObject will for sure). Things to try:

              • return a pointer to your gadget, not a copy
              • declare pointer to your gadget as Qt meta type, register it with QML

              (Z(:^

              1 Reply Last reply
              1
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                You can't have a Q_GADGET and a Q_OBJECT in the same header file, moc is not smart enough to process that.

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                S 1 Reply Last reply
                3
                • S Offline
                  S Offline
                  seyed
                  wrote on last edited by
                  #8

                  1- your struct or simple class must have Q_GADGET as minimum
                  2- you should declare properties in order to access from qml
                  2- you must declare your struct/class by Q_DECLARE_METATYPE()
                  3- you must register it using qRegisterMetaType<>() somewhere before loading qml file by engine such as main.cpp

                  so you have something like this:

                  struct MyStruct {
                      Q_GADGET
                      Q_PROPERTY(QString str1 MEMBER m_str1)
                  public:
                      QString m_str1;
                  };
                  Q_DECLARE_METATYPE(MyStruct)
                  

                  main.cpp

                  //...
                  qRegisterMetaType<MyStruct>();
                  //...
                  engine.load(url);
                  //...
                  

                  NOTE: be careful, is seems that nested classes/struct not supported by Qt meta

                  1 Reply Last reply
                  0
                  • VRoninV VRonin

                    You can't have a Q_GADGET and a Q_OBJECT in the same header file, moc is not smart enough to process that.

                    S Offline
                    S Offline
                    seyed
                    wrote on last edited by
                    #9

                    @VRonin same header is not problem, but it seems that nested struct/classes with Q_GADGET and Q_OBJECT is not supported.

                    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