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. C++ return to QML
Forum Updated to NodeBB v4.3 + New Features

C++ return to QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 3 Posters 1.0k Views 3 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #1

    I have a C++ API function that looks up a particular string to find an object, it returns a reference to the QML. What can I return to the QML if the reference doesn't correspond to any found object ?

    Kind Regards,
    Sy

    SPlattenS 1 Reply Last reply
    0
    • SPlattenS SPlatten

      Can anyone point / help me to a good example / tutorial on using QVariant to construct an object that can be returned to QML ?

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #6

      @SPlatten said in C++ return to QML:

      Can anyone point / help me to a good example / tutorial on using QVariant to construct an object that can be returned to QML ?

      For your type to be accessible in QML it is either a primitive, a gadget (a.k.a. a value type) or a QObject derived class. No other options are allowed. If it's a primitive - it's directly accessible. If it's a gadget then it must qualify for a metatype and has to have the Q_GADGET macro, (usually) have it registered with qRegisterMetatype if it's a parameter for signal/slot connections, and most importantly be registered with the QML engine with QML_VALUE_TYPE.
      https://doc.qt.io/qt-6/qtqml-cppintegration-definetypes.html

      QObject derived types simply have the Q_OBJECT macro and the QML_ELEMENT annotation.

      Either way you should declare the available properties with Q_PROPERTY.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      5
      • SPlattenS SPlatten

        I have a C++ API function that looks up a particular string to find an object, it returns a reference to the QML. What can I return to the QML if the reference doesn't correspond to any found object ?

        SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by SPlatten
        #2

        I believe I've solved the issue, I've added a local static to the function that is called by the QML:

        staledata_t& staledata_t::isRegistered(QString strUDT_) {
            staledata_t* pobjData(staledata_t::pFindTag(strUDT_));
            static staledata_t invalid;
            if ( pobjData != nullptr ) {
                return *pobjData;
            }
            return invalid;
        }
        

        The invalid variable will only be valid if it has all the required parameters set-up. That works nicely, the next issue, is how can I return an object to the QML that it can interpret? Is JSON an option ?

        Kind Regards,
        Sy

        JoeCFDJ 1 Reply Last reply
        0
        • SPlattenS SPlatten

          I believe I've solved the issue, I've added a local static to the function that is called by the QML:

          staledata_t& staledata_t::isRegistered(QString strUDT_) {
              staledata_t* pobjData(staledata_t::pFindTag(strUDT_));
              static staledata_t invalid;
              if ( pobjData != nullptr ) {
                  return *pobjData;
              }
              return invalid;
          }
          

          The invalid variable will only be valid if it has all the required parameters set-up. That works nicely, the next issue, is how can I return an object to the QML that it can interpret? Is JSON an option ?

          JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on last edited by JoeCFD
          #3

          @SPlatten if you want to pass the object back to QML to do something, why can not you call the object inside staledata_t to do what the object does inside QML?

          Also isRegistered() has to be static?

          SPlattenS 1 Reply Last reply
          0
          • JoeCFDJ JoeCFD

            @SPlatten if you want to pass the object back to QML to do something, why can not you call the object inside staledata_t to do what the object does inside QML?

            Also isRegistered() has to be static?

            SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on last edited by SPlatten
            #4

            @JoeCFD , thank you, no isRegistered isn't static, prototype:

            Q_INVOKABLE staledata_t& isRegistered(QString strUDT_);
            

            I think I can create a custom type using QVariant, will look into this further. BTW I said pass not parse, I don't need to manage the passed back object in C++, just return the found data.

            Kind Regards,
            Sy

            SPlattenS 1 Reply Last reply
            0
            • SPlattenS SPlatten

              @JoeCFD , thank you, no isRegistered isn't static, prototype:

              Q_INVOKABLE staledata_t& isRegistered(QString strUDT_);
              

              I think I can create a custom type using QVariant, will look into this further. BTW I said pass not parse, I don't need to manage the passed back object in C++, just return the found data.

              SPlattenS Offline
              SPlattenS Offline
              SPlatten
              wrote on last edited by
              #5

              Can anyone point / help me to a good example / tutorial on using QVariant to construct an object that can be returned to QML ?

              Kind Regards,
              Sy

              kshegunovK 1 Reply Last reply
              0
              • SPlattenS SPlatten

                Can anyone point / help me to a good example / tutorial on using QVariant to construct an object that can be returned to QML ?

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #6

                @SPlatten said in C++ return to QML:

                Can anyone point / help me to a good example / tutorial on using QVariant to construct an object that can be returned to QML ?

                For your type to be accessible in QML it is either a primitive, a gadget (a.k.a. a value type) or a QObject derived class. No other options are allowed. If it's a primitive - it's directly accessible. If it's a gadget then it must qualify for a metatype and has to have the Q_GADGET macro, (usually) have it registered with qRegisterMetatype if it's a parameter for signal/slot connections, and most importantly be registered with the QML engine with QML_VALUE_TYPE.
                https://doc.qt.io/qt-6/qtqml-cppintegration-definetypes.html

                QObject derived types simply have the Q_OBJECT macro and the QML_ELEMENT annotation.

                Either way you should declare the available properties with Q_PROPERTY.

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                5

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved