Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to use qRegisterMetaType to solve this issue?



  • Originally I was passing a pointer to a type to signals which worked, the slots obviously had to returned the same pointer, again this worked.

    However I thought it would be safer and an improvement to pass a reference instead. However this results in the message:

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

    So I looked at the documentation for qRegisterMetaType and added:

    void clsJSON::typeRegistration() {
       qRegisterMetaType<QJsonObject&>("QJsonObject&");
    }
    

    This doesn't compile and results in:

    /Users/sy/XMLMPAM/clsJSON.cpp:301: error: no matching function for call to 'qRegisterMetaType'
    ../clsJSON.cpp:301:4: error: no matching function for call to 'qRegisterMetaType'
       qRegisterMetaType<QJsonObject&>("QJsonObject&");
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ../../Qt/5.15.2/clang_64/lib/QtCore.framework/Headers/qmetatype.h:1884:5: note: candidate template ignored: substitution failure [with T = QJsonObject &]: 'dummy' declared as a pointer to a reference of type 'QJsonObject &'
    int qRegisterMetaType(const char *typeName
        ^
    ../../Qt/5.15.2/clang_64/lib/QtCore.framework/Headers/qmetatype.h:1933:43: note: candidate template ignored: substitution failure [with T = QJsonObject &]: 'type name' declared as a pointer to a reference of type 'QJsonObject &'
    QT_DEPRECATED inline Q_DECL_CONSTEXPR int qRegisterMetaType(T *)
                                              ^                   ~
    ../../Qt/5.15.2/clang_64/lib/QtCore.framework/Headers/qmetatype.h:1921:29: note: candidate function template not viable: requires 0 arguments, but 1 was provided
    inline Q_DECL_CONSTEXPR int qRegisterMetaType()
                                ^
    

    I have already included:

        #include <QMetaType>
    

    How do I resolve the initial problem or do I have to stick with a pointer? I really don't want to pass the entire QJsonObject on the stack so a pointer or reference is preferable.

    I then tried:

    typedef QJsonObject& rQJsonObject;
    

    and:

    qRegisterMetaType<rQJsonObject>("rQJsonObject");
    

    With this:

    no matching function for call to 'qRegisterMetaType'
    

    Appears to the right of the line.


  • Moderators

    @SPlatten said in How to use qRegisterMetaType to solve this issue?:

    However I thought it would be safer and an improvement to pass a reference instead.

    This is not true. Passing a reference has the same dangers as passing a pointer.

    How to use qRegisterMetaType to solve this issue?

    You can't use qRegisterMetaType to solve the issue. You pass by const reference instead to solve the issue.

    Also, make sure you understand the top answer at https://stackoverflow.com/questions/2139224/how-to-pass-objects-to-functions-in-c


Log in to reply