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

passing Q_GADGET object as argument

Scheduled Pinned Locked Moved Unsolved C++ Gurus
7 Posts 3 Posters 800 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.
  • M Offline
    M Offline
    mzimmers
    wrote on 1 Dec 2023, 19:49 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...

    C 1 Reply Last reply 1 Dec 2023, 21:34
    0
    • M mzimmers
      1 Dec 2023, 19:49

      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...

      C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 1 Dec 2023, 21:34 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

      M 1 Reply Last reply 1 Dec 2023, 21:44
      0
      • C Christian Ehrlicher
        1 Dec 2023, 21:34

        @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.

        M Offline
        M Offline
        mzimmers
        wrote on 1 Dec 2023, 21:44 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.

        C 1 Reply Last reply 1 Dec 2023, 21:53
        0
        • M mzimmers
          1 Dec 2023, 21:44

          @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.

          C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 1 Dec 2023, 21:53 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

          M 1 Reply Last reply 1 Dec 2023, 21:59
          0
          • C Christian Ehrlicher
            1 Dec 2023, 21:53

            @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.

            M Offline
            M Offline
            mzimmers
            wrote on 1 Dec 2023, 21:59 last edited by
            #5

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

            A 1 Reply Last reply 1 Dec 2023, 22:25
            0
            • M mzimmers
              1 Dec 2023, 21:59

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

              A Offline
              A Offline
              Axel Spoerl
              Moderators
              wrote on 1 Dec 2023, 22:25 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

              M 1 Reply Last reply 1 Dec 2023, 22:50
              0
              • A Axel Spoerl
                1 Dec 2023, 22:25

                @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?

                M Offline
                M Offline
                mzimmers
                wrote on 1 Dec 2023, 22:50 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

                2/7

                1 Dec 2023, 21:34

                topic:navigator.unread, 5
                • Login

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