Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. passing Q_GADGET object as argument

passing Q_GADGET object as argument

Scheduled Pinned Locked Moved Unsolved C++ Gurus
7 Posts 3 Posters 752 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

    Hi all -

    I tried to code a C++ routine that would be invoked from QML. One of the arguments is a struct I've written that uses Q_GADGET. When I try to pass this by reference, I get the following errors:

    static assertion failed: Meta Types cannot be non-const references or rvalue references.
    forming pointer to reference type 'Equipment&'
    

    I did some searching, and I think I understand the reason. My question: does the second error message I got suggest that Qt is creating a pointer for me (which would render my attempt superfluous)?

    The called function does need to modify the contents of this struct, so this has implications for what to do if the answer is "no."

    Thanks...

    Christian EhrlicherC 1 Reply Last reply
    0
    • mzimmersM mzimmers

      Hi all -

      I tried to code a C++ routine that would be invoked from QML. One of the arguments is a struct I've written that uses Q_GADGET. When I try to pass this by reference, I get the following errors:

      static assertion failed: Meta Types cannot be non-const references or rvalue references.
      forming pointer to reference type 'Equipment&'
      

      I did some searching, and I think I understand the reason. My question: does the second error message I got suggest that Qt is creating a pointer for me (which would render my attempt superfluous)?

      The called function does need to modify the contents of this struct, so this has implications for what to do if the answer is "no."

      Thanks...

      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @mzimmers said in passing Q_GADGET object as argument:

      The called function does need to modify the contents of this struct,

      So fix this design flaw - signals and slots are not meant to modify the passsed function so the calling function (signal) is using the modified object afterwards. It can not work since there is e.g. Qt::QueuedConnections.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      mzimmersM 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        @mzimmers said in passing Q_GADGET object as argument:

        The called function does need to modify the contents of this struct,

        So fix this design flaw - signals and slots are not meant to modify the passsed function so the calling function (signal) is using the modified object afterwards. It can not work since there is e.g. Qt::QueuedConnections.

        mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by
        #3

        @Christian-Ehrlicher I'm not using signals and slots - this is a Q_GADGET, not a Q_OBJECT. I merely coded a Q_INVOKABLE routine that is called from the QML, to modify the model contents.

        Christian EhrlicherC 1 Reply Last reply
        0
        • mzimmersM mzimmers

          @Christian-Ehrlicher I'm not using signals and slots - this is a Q_GADGET, not a Q_OBJECT. I merely coded a Q_INVOKABLE routine that is called from the QML, to modify the model contents.

          Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @mzimmers said in passing Q_GADGET object as argument:

          merely coded a Q_INVOKABLE routine that is called from the QML,

          This is not different from a signal slot connection. Fix your design.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          mzimmersM 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            @mzimmers said in passing Q_GADGET object as argument:

            merely coded a Q_INVOKABLE routine that is called from the QML,

            This is not different from a signal slot connection. Fix your design.

            mzimmersM Offline
            mzimmersM Offline
            mzimmers
            wrote on last edited by
            #5

            @Christian-Ehrlicher what is wrong with a design that invokes C++ methods to operate on data used in QML?

            Axel SpoerlA 1 Reply Last reply
            0
            • mzimmersM mzimmers

              @Christian-Ehrlicher what is wrong with a design that invokes C++ methods to operate on data used in QML?

              Axel SpoerlA Offline
              Axel SpoerlA Offline
              Axel Spoerl
              Moderators
              wrote on last edited by
              #6

              @mzimmers
              Do I get this right: The intention is to pass a function pointer as an argument, and you want to use the macros designed for signal/slot connections to derive that pointer from a string? It doesn’t work, because the macros haven’t been designed for that purpose. It would be possible to plumb that functionality around QMetaObject. But that’ll process a string at runtime, while you could use the function pointer directly. What’s the reason for discarding that way?

              Software Engineer
              The Qt Company, Oslo

              mzimmersM 1 Reply Last reply
              0
              • Axel SpoerlA Axel Spoerl

                @mzimmers
                Do I get this right: The intention is to pass a function pointer as an argument, and you want to use the macros designed for signal/slot connections to derive that pointer from a string? It doesn’t work, because the macros haven’t been designed for that purpose. It would be possible to plumb that functionality around QMetaObject. But that’ll process a string at runtime, while you could use the function pointer directly. What’s the reason for discarding that way?

                mzimmersM Offline
                mzimmersM Offline
                mzimmers
                wrote on last edited by
                #7

                @Axel-Spoerl no, not at all. Maybe some code will help. I have a struct:

                struct Equipment
                {
                    Q_GADGET
                    QML_STRUCTURED_VALUE
                    QML_VALUE_TYPE(equipment)
                    ...
                

                and I maintain a list of these in a model.

                In my QML, I have a ListView that accesses the model, and furnishes the contents to a delegate that eventually gets down to:

                equipmentModel.processSpaceAssignment(vspInModel, spaceUuid, checked)
                

                where vspInModel is an instance of a subclass of Equipment. There's some rather complex work that needs to be done, so I didn't want to try to implement it in JS. The C++ function signature is:

                bool EquipmentModel::processSpaceAssignment(Equipment equipment, QUuid spaceUuid, bool add) 
                

                So, my original question was, is this equipment/vsp object passed by value in this case?

                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