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. Q_DECLARE_METATYPE and qRegisterMetaType?

Q_DECLARE_METATYPE and qRegisterMetaType?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 8.2k 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.
  • E Offline
    E Offline
    EStudley
    wrote on last edited by EStudley
    #1

    Hello all,

    I am using Qt 5.4.2. I have an enum, ClientError, that I am trying to use in several ways:

    • A meta type for use with QVariants in a QAbstractListModel:
     errorVariant.canConvert<ErrorHandler::ClientError>()
    
    • A function parameter:
    ErrorModel::addError(ErrorHandler::ClientError)
    
    • A QML enum:
    errorModel.addError(ClientError.MajorError)
    

    ErrorHandler.h contains the following:

    class ErrorHandler : public QObject
    {
        Q_OBJECT
        Q_ENUMS(ClientError)
    public:
        enum ClientError
        {
            MajorError,
    ...
    ...
    };
    
    Q_DECLARE_METATYPE(ErrorHandler::ClientError)
    
    #endif // ERRORHANDLER_H
    

    Main.cpp contains the following:

    #include <client/ErrorHandler.h>
    
    int main(int argc, char **argv)
    {
    ...
    ...
        qRegisterMetaType<ErrorHandler::ClientError>("ErrorHandler::ClientError");
        qmlRegisterUncreatableType<ErrorHandler>("Client", 1, 0, "ClientError","Display Client Errors");
    

    When I build, I get the following:

    main.obj:-1: error: LNK2019: unresolved external symbol "public: static struct QMetaObject const ErrorHandler::staticMetaObject" (?staticMetaObject@ErrorHandler@@2UQMetaObject@@B) referenced in function "int __cdecl qRegisterNormalizedMetaType<class ErrorHandler *>(class QByteArray const &,class ErrorHandler * *,enum QtPrivate::MetaTypeDefinedHelper<class ErrorHandler *,1>::DefinedType)" (??$qRegisterNormalizedMetaType@PEAVErrorHandler@@@@YAHAEBVQByteArray@@PEAPEAVErrorHandler@@W4DefinedType@?$MetaTypeDefinedHelper@PEAVErrorHandler@@$00@QtPrivate@@@Z)
    

    Am I doing anything wrong? Is it possible to double-use an enum in this way?

    Thanks.

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      Q_ENUMS is deprecated, use Q_ENUM that already includes the Q_DECLARE_METATYPE so no need to explicitly add it. Also you don't need to register QObject derived types

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      1
      • E Offline
        E Offline
        EStudley
        wrote on last edited by
        #3

        I'm currently using Qt 5.4.2; it seems like that's a newer feature.

        kshegunovK 1 Reply Last reply
        0
        • E EStudley

          I'm currently using Qt 5.4.2; it seems like that's a newer feature.

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

          Is this a library? I mean where you define the ErrorHandler class. If so you need to export that class from it.

          PS. Also use qRegisterMetaType without a string argument:

          qRegisterMetaType<ErrorHandler::ClientError>();
          

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          2
          • E Offline
            E Offline
            EStudley
            wrote on last edited by
            #5

            By export you mean "CONFIG += client" in clientApp.pro, correct?

            I tried without a string argument, same result.

            kshegunovK 1 Reply Last reply
            0
            • E EStudley

              By export you mean "CONFIG += client" in clientApp.pro, correct?

              I tried without a string argument, same result.

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

              Not at all, unless your library is a module QT += is not going to work and CONFIG += is for qmake features/configurations so it has no place here. I mean like described here.

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              0
              • E Offline
                E Offline
                EStudley
                wrote on last edited by EStudley
                #7

                It is a library and it's exported correctly. I am able to access a different enum from a different class in the same library from QML; the only difference is me using Q_DECLARE_METATYPE with ClientError.

                kshegunovK 1 Reply Last reply
                0
                • E EStudley

                  It is a library and it's exported correctly. I am able to access a different enum from a different class in the same library from QML; the only difference is me using Q_DECLARE_METATYPE with ClientError.

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

                  @EStudley said in Q_DECLARE_METATYPE and qRegisterMetaType?:
                  Firstly, you shouldn't need to use Q_DECLARE_METATYPE (or qRegisterMetaType) with the enum at all, this is handled by the moc. Here you can see that the macro expands to nothing.
                  Secondly, your linker (MSVC) complains that it can't find the metaObject for your ErrorHandler enum, which is normal as it is not exported from the binary by Q_DECLARE_METATYPE.

                  In conclusion:
                  Don't use Q_DECLARE_METATYPE and qRegisterMetaType for your QObject enum. Use it if you want to use it as a global enumerator and then you need to call the meta-type runtime registration from the library, not from the application.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  1
                  • E Offline
                    E Offline
                    EStudley
                    wrote on last edited by
                    #9

                    Thanks! That seemed to solve my issues.

                    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