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. Correct syntax to register object reference using qRegisterMetaType?

Correct syntax to register object reference using qRegisterMetaType?

Scheduled Pinned Locked Moved Solved General and Desktop
34 Posts 7 Posters 6.1k 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.
  • SPlattenS SPlatten

    I had a problem with a signal and slot which I found was reported as:

    QObject::connect: Cannot queue arguments of type 'QJsonObject&'
    (Make sure 'QJsonObject&' is registered using qRegisterMetaType().)
    

    What is the correct syntax to use to register this type?

    I've tried:

    qRegisterMetaType<QJsonObject&>("QJsonObject&");
    

    The result is a red underline on qRegisterMetaType and to the right:

    no matching function for call to 'qRegisterMetaType'
    

    I then tried:

    qRegisterMetaType<QJsonObject>("QJsonObject");
    

    The red underline and message disappear but the original message is still displayed, so how do I register the reference to the type?

    SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by SPlatten
    #2
    This post is deleted!
    1 Reply Last reply
    0
    • SPlattenS SPlatten

      I had a problem with a signal and slot which I found was reported as:

      QObject::connect: Cannot queue arguments of type 'QJsonObject&'
      (Make sure 'QJsonObject&' is registered using qRegisterMetaType().)
      

      What is the correct syntax to use to register this type?

      I've tried:

      qRegisterMetaType<QJsonObject&>("QJsonObject&");
      

      The result is a red underline on qRegisterMetaType and to the right:

      no matching function for call to 'qRegisterMetaType'
      

      I then tried:

      qRegisterMetaType<QJsonObject>("QJsonObject");
      

      The red underline and message disappear but the original message is still displayed, so how do I register the reference to the type?

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

      I thought I'd found the issue, removing the String parameter and replacing the call with:

      qRegisterMetaType<QJsonObject&>();
      

      Is accepted and there is no red underline or error message to the right, but when compiling the messages displayed are:

      /Users/sy/Qt/5.15.2/clang_64/lib/QtCore.framework/Headers/qmetatype.h:1916: error: static_assert failed due to requirement 'bool(QMetaTypeId2<QJsonObject &>::Defined)' "Type is not registered, please use the Q_DECLARE_METATYPE macro to make it known to Qt's meta-object system"
      In file included from ../clsMsgSender.cpp:1:
      In file included from ../../Qt/5.15.2/clang_64/lib/QtCore.framework/Headers/QMetaType:1:
      /Users/sy/Qt/5.15.2/clang_64/lib/QtCore.framework/Headers/qmetatype.h:1916:5: error: static_assert failed due to requirement 'bool(QMetaTypeId2<QJsonObject &>::Defined)' "Type is not registered, please use the Q_DECLARE_METATYPE macro to make it known to Qt's meta-object system"
          Q_STATIC_ASSERT_X(QMetaTypeId2<T>::Defined, "Type is not registered, please use the Q_DECLARE_METATYPE macro to make it known to Qt's meta-object system");
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      /Users/sy/Qt/5.15.2/clang_64/lib/QtCore.framework/Headers/qglobal.h:121:49: note: expanded from macro 'Q_STATIC_ASSERT_X'
      #  define Q_STATIC_ASSERT_X(Condition, Message) static_assert(bool(Condition), Message)
                                                      ^             ~~~~~~~~~~~~~~~
      /Users/sy/Qt/5.15.2/clang_64/lib/QtCore.framework/Headers/qmetatype.h:1923:12: note: in instantiation of function template specialization 'qMetaTypeId<QJsonObject &>' requested here
          return qMetaTypeId<T>();
                 ^
      ../clsMsgSender.cpp:20:5: note: in instantiation of function template specialization 'qRegisterMetaType<QJsonObject &>' requested here
          qRegisterMetaType<QJsonObject&>();
          ^
      

      After searching online I tried adding:

      Q_DECLARE_METATYPE(QJsonObject&)
      

      This results in:

      'type name' declared as a pointer to a reference of type 'QJsonObject &'
      

      Only removing the & gets rid of this but then I'm back to the original problem, so how do I get over this?
      I had to remove the &

      SPlattenS 1 Reply Last reply
      0
      • SPlattenS SPlatten

        I thought I'd found the issue, removing the String parameter and replacing the call with:

        qRegisterMetaType<QJsonObject&>();
        

        Is accepted and there is no red underline or error message to the right, but when compiling the messages displayed are:

        /Users/sy/Qt/5.15.2/clang_64/lib/QtCore.framework/Headers/qmetatype.h:1916: error: static_assert failed due to requirement 'bool(QMetaTypeId2<QJsonObject &>::Defined)' "Type is not registered, please use the Q_DECLARE_METATYPE macro to make it known to Qt's meta-object system"
        In file included from ../clsMsgSender.cpp:1:
        In file included from ../../Qt/5.15.2/clang_64/lib/QtCore.framework/Headers/QMetaType:1:
        /Users/sy/Qt/5.15.2/clang_64/lib/QtCore.framework/Headers/qmetatype.h:1916:5: error: static_assert failed due to requirement 'bool(QMetaTypeId2<QJsonObject &>::Defined)' "Type is not registered, please use the Q_DECLARE_METATYPE macro to make it known to Qt's meta-object system"
            Q_STATIC_ASSERT_X(QMetaTypeId2<T>::Defined, "Type is not registered, please use the Q_DECLARE_METATYPE macro to make it known to Qt's meta-object system");
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        /Users/sy/Qt/5.15.2/clang_64/lib/QtCore.framework/Headers/qglobal.h:121:49: note: expanded from macro 'Q_STATIC_ASSERT_X'
        #  define Q_STATIC_ASSERT_X(Condition, Message) static_assert(bool(Condition), Message)
                                                        ^             ~~~~~~~~~~~~~~~
        /Users/sy/Qt/5.15.2/clang_64/lib/QtCore.framework/Headers/qmetatype.h:1923:12: note: in instantiation of function template specialization 'qMetaTypeId<QJsonObject &>' requested here
            return qMetaTypeId<T>();
                   ^
        ../clsMsgSender.cpp:20:5: note: in instantiation of function template specialization 'qRegisterMetaType<QJsonObject &>' requested here
            qRegisterMetaType<QJsonObject&>();
            ^
        

        After searching online I tried adding:

        Q_DECLARE_METATYPE(QJsonObject&)
        

        This results in:

        'type name' declared as a pointer to a reference of type 'QJsonObject &'
        

        Only removing the & gets rid of this but then I'm back to the original problem, so how do I get over this?
        I had to remove the &

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

        @SPlatten , Removing the & so the signal and slot parameters are simply QJsonObject compiles and works, but I'm concerned that the could use more stack and a reference. I replaced the parameter again with a pointer and this works fine to.

        JKSHJ 1 Reply Last reply
        0
        • SPlattenS SPlatten

          @SPlatten , Removing the & so the signal and slot parameters are simply QJsonObject compiles and works, but I'm concerned that the could use more stack and a reference. I replaced the parameter again with a pointer and this works fine to.

          JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by JKSH
          #5

          @SPlatten said in Correct syntax to register object reference using qRegisterMetaType?:

          I'm concerned that the could use more stack and a reference.

          There is no need for concern. QJsonObject (and many other Qt datatypes) are implicitly shared so copying is cheap: https://doc.qt.io/qt-5/implicit-sharing.html

          I replaced the parameter again with a pointer and this works fine to.

          It is easy to cause crashes this way.

          How do you manage the lifetime of your object?

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          SPlattenS kshegunovK 2 Replies Last reply
          3
          • JKSHJ JKSH

            @SPlatten said in Correct syntax to register object reference using qRegisterMetaType?:

            I'm concerned that the could use more stack and a reference.

            There is no need for concern. QJsonObject (and many other Qt datatypes) are implicitly shared so copying is cheap: https://doc.qt.io/qt-5/implicit-sharing.html

            I replaced the parameter again with a pointer and this works fine to.

            It is easy to cause crashes this way.

            How do you manage the lifetime of your object?

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

            @JKSH , can you please explain what you mean?

            JKSHJ 1 Reply Last reply
            0
            • SPlattenS SPlatten

              @JKSH , can you please explain what you mean?

              JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by JKSH
              #7

              @SPlatten said in Correct syntax to register object reference using qRegisterMetaType?:

              @JKSH , can you please explain what you mean?

              Sure thing.

              Which part would you like an explanation on? Please have a read through my link first, then let me know which specific parts are unclear.

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              0
              • JKSHJ JKSH

                @SPlatten said in Correct syntax to register object reference using qRegisterMetaType?:

                I'm concerned that the could use more stack and a reference.

                There is no need for concern. QJsonObject (and many other Qt datatypes) are implicitly shared so copying is cheap: https://doc.qt.io/qt-5/implicit-sharing.html

                I replaced the parameter again with a pointer and this works fine to.

                It is easy to cause crashes this way.

                How do you manage the lifetime of your object?

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

                @JKSH said in Correct syntax to register object reference using qRegisterMetaType?:

                It is easy to cause crashes this way.

                Yeah, what the other guy said ...

                @SPlatten said in Correct syntax to register object reference using qRegisterMetaType?:

                Only removing the & gets rid of this but then I'm back to the original problem, so how do I get over this?

                You don't. You shall not register references as meta-types. They can't ever be such, because they can't be copied.

                https://doc.qt.io/qt-5/qmetatype.html#details

                Declare new types with Q_DECLARE_METATYPE() to make them available to QVariant and other template-based functions. Call qRegisterMetaType() to make types available to non-template based functions, such as the queued signal and slot connections.

                Any class or struct that has a public default constructor, a public copy constructor, and a public destructor can be registered.

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                3
                • SPlattenS Offline
                  SPlattenS Offline
                  SPlatten
                  wrote on last edited by SPlatten
                  #9

                  This is still an issue, I am passing references to signals, type: QJsonObject&, I found this post:
                  https://stackoverflow.com/questions/23219770/how-to-register-reference-to-qt-containers

                  However it hasn't helped, I added a call to:

                  qRegisterMetaType<QJsonObject>("QJsonObject");
                  

                  I'm still getting:

                  QObject::connect: Cannot queue arguments of type 'QJsonObject&'
                  

                  Note I haven't put in the & in the registration as the post on stackoverflow advised against it.

                  As far as I can see the code is working regardless of the warning, I just don't like messages like this because they suggest something is wrong, can I disable the warning?

                  Where I have these types the connects have been modified having the last parameter as , Qt::DirectConnection

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    Hi,

                    Don't pass references in parameters. As @JKSH wrote earlier, most of Qt datatypes are implicitly shared so there's really no need to try to go that way.

                    If you are forcing the Qt::DirectConnection because of that then it means you have a design issue. It's currently pretty rare to have to do that and it also has consequences when doing multithreading.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    SPlattenS 1 Reply Last reply
                    1
                    • SGaistS SGaist

                      Hi,

                      Don't pass references in parameters. As @JKSH wrote earlier, most of Qt datatypes are implicitly shared so there's really no need to try to go that way.

                      If you are forcing the Qt::DirectConnection because of that then it means you have a design issue. It's currently pretty rare to have to do that and it also has consequences when doing multithreading.

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

                      @SGaist , I want to pass by reference because my understanding of passing by reference is that it is far less costly on stack space. A QJsonObject can be very large.

                      kshegunovK 1 Reply Last reply
                      0
                      • SPlattenS SPlatten

                        @SGaist , I want to pass by reference because my understanding of passing by reference is that it is far less costly on stack space. A QJsonObject can be very large.

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

                        @SPlatten said in Correct syntax to register object reference using qRegisterMetaType?:

                        I want to pass by reference because my understanding of passing by reference is that it is far less costly on stack space. A QJsonObject can be very large.

                        Your understanding is irrelevant in this case. You were told it isn't possible with a queued connection, and it still isn't, and it is not going to be, no matter how much we argue about it. The warning is there for a reason - to warn you that you have a serious bug in your code. You can either use a direct connection and suffer the inevitable consequences of that, or use const QJsonObject &/QJsonObject for your signal arguments.

                        Read and abide by the Qt Code of Conduct

                        SPlattenS 1 Reply Last reply
                        1
                        • kshegunovK kshegunov

                          @SPlatten said in Correct syntax to register object reference using qRegisterMetaType?:

                          I want to pass by reference because my understanding of passing by reference is that it is far less costly on stack space. A QJsonObject can be very large.

                          Your understanding is irrelevant in this case. You were told it isn't possible with a queued connection, and it still isn't, and it is not going to be, no matter how much we argue about it. The warning is there for a reason - to warn you that you have a serious bug in your code. You can either use a direct connection and suffer the inevitable consequences of that, or use const QJsonObject &/QJsonObject for your signal arguments.

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

                          @kshegunov , I am using const QJsonObject& for my signal arguments, these are working, the only thing that is displayed in the Application Output is the message I've raised. I'm not experience any other problems, crashes or bugs.

                          JonBJ SGaistS 2 Replies Last reply
                          0
                          • SPlattenS SPlatten

                            @kshegunov , I am using const QJsonObject& for my signal arguments, these are working, the only thing that is displayed in the Application Output is the message I've raised. I'm not experience any other problems, crashes or bugs.

                            JonBJ Online
                            JonBJ Online
                            JonB
                            wrote on last edited by
                            #14

                            @SPlatten
                            The signal argument type is "OK", insofar as it would work OK with non-cross-thread direct connection connect()s. But the problem is that you are trying to use it when you need a queued connection. That is why you get the error message on the QObject::connect statement.

                            SPlattenS 1 Reply Last reply
                            1
                            • JonBJ JonB

                              @SPlatten
                              The signal argument type is "OK", insofar as it would work OK with non-cross-thread direct connection connect()s. But the problem is that you are trying to use it when you need a queued connection. That is why you get the error message on the QObject::connect statement.

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

                              @JonB , thank you, I'll take a look.

                              1 Reply Last reply
                              0
                              • SPlattenS SPlatten

                                @kshegunov , I am using const QJsonObject& for my signal arguments, these are working, the only thing that is displayed in the Application Output is the message I've raised. I'm not experience any other problems, crashes or bugs.

                                SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #16

                                @SPlatten said in Correct syntax to register object reference using qRegisterMetaType?:

                                @kshegunov , I am using const QJsonObject& for my signal arguments, these are working, the only thing that is displayed in the Application Output is the message I've raised. I'm not experience any other problems, crashes or bugs.

                                For the slots as well ?

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                SPlattenS 1 Reply Last reply
                                2
                                • SGaistS SGaist

                                  @SPlatten said in Correct syntax to register object reference using qRegisterMetaType?:

                                  @kshegunov , I am using const QJsonObject& for my signal arguments, these are working, the only thing that is displayed in the Application Output is the message I've raised. I'm not experience any other problems, crashes or bugs.

                                  For the slots as well ?

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

                                  @SGaist Of course.

                                  1 Reply Last reply
                                  0
                                  • SPlattenS Offline
                                    SPlattenS Offline
                                    SPlatten
                                    wrote on last edited by
                                    #18

                                    Ok, I've now got rid of the warning, by going through all the QObject::connect calls and only using Qt::DirectionConnection where the signal and slot pointers are the same.

                                    JonBJ 1 Reply Last reply
                                    0
                                    • SPlattenS SPlatten

                                      Ok, I've now got rid of the warning, by going through all the QObject::connect calls and only using Qt::DirectionConnection where the signal and slot pointers are the same.

                                      JonBJ Online
                                      JonBJ Online
                                      JonB
                                      wrote on last edited by
                                      #19

                                      @SPlatten
                                      A lot of your questions are about threads. Are your connect()s' senders/receivers never in separate threads?

                                      SPlattenS 1 Reply Last reply
                                      0
                                      • JonBJ JonB

                                        @SPlatten
                                        A lot of your questions are about threads. Are your connect()s' senders/receivers never in separate threads?

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

                                        @JonB , not for the ones where I've used Qt::DirectConnection

                                        1 Reply Last reply
                                        1
                                        • Christian EhrlicherC Offline
                                          Christian EhrlicherC Offline
                                          Christian Ehrlicher
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #21

                                          @SPlatten said in Correct syntax to register object reference using qRegisterMetaType?:

                                          not for the ones where I've used Qt::DirectConnection

                                          Then it's useless since then Qt::AutoConnection evaluates to Qt::DirectConnection. Specifying a connection type is not needed in most of the cases.

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

                                          SPlattenS 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