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. Register Qt type as meta type
Qt 6.11 is out! See what's new in the release blog

Register Qt type as meta type

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 2.3k Views 1 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.
  • P Offline
    P Offline
    PaulNewman
    wrote on last edited by
    #1

    Can Qt types can be register in meta object system? I tried to register enum QtMsgType in class inherited from QObject with qobject macro and qenum macro and in class with qgadget. *QMetaEnum::enumName() returns nullptr in both cases but compiler doesnt say something about what i do wrong. QMetaObject::indexOfEnumerator returns -1 but i register type.

    class Logging
    {
        Q_GADGET
        Q_ENUM(QtMsgType);
    ...
    public:
        void static messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg);
    // all other methods are static
    }
    
    void Logging::messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
    {
    ...
        auto metaEnum = QMetaEnum::fromType<QtMsgType>;
        auto value = metaEnum().enumName(); // nullptr
        auto variant = QVariant::fromValue(type); //null variant
    ...
    }
    
    kshegunovK 1 Reply Last reply
    0
    • gde23G Offline
      gde23G Offline
      gde23
      wrote on last edited by
      #2

      when you want to use your own type as meta type you need to declare it as meta type with the macro

      Q_DECLARE_METATYPE(YourType);

      have you done that?

      1 Reply Last reply
      1
      • P PaulNewman

        Can Qt types can be register in meta object system? I tried to register enum QtMsgType in class inherited from QObject with qobject macro and qenum macro and in class with qgadget. *QMetaEnum::enumName() returns nullptr in both cases but compiler doesnt say something about what i do wrong. QMetaObject::indexOfEnumerator returns -1 but i register type.

        class Logging
        {
            Q_GADGET
            Q_ENUM(QtMsgType);
        ...
        public:
            void static messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg);
        // all other methods are static
        }
        
        void Logging::messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
        {
        ...
            auto metaEnum = QMetaEnum::fromType<QtMsgType>;
            auto value = metaEnum().enumName(); // nullptr
            auto variant = QVariant::fromValue(type); //null variant
        ...
        }
        
        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #3

        If your enum is contained in your class - use Q_ENUM and that's enough to declare it as metatype. Otherwise use Q_DECLARE_METATYPE as @gde23 said.

        Read and abide by the Qt Code of Conduct

        P 1 Reply Last reply
        0
        • kshegunovK kshegunov

          If your enum is contained in your class - use Q_ENUM and that's enough to declare it as metatype. Otherwise use Q_DECLARE_METATYPE as @gde23 said.

          P Offline
          P Offline
          PaulNewman
          wrote on last edited by
          #4

          @kshegunov i tried this too, it didnt work. I think your missed what my enum really is. It's not mine enum, its Qt enum.

          kshegunovK 1 Reply Last reply
          0
          • P PaulNewman

            @kshegunov i tried this too, it didnt work. I think your missed what my enum really is. It's not mine enum, its Qt enum.

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

            @PaulNewman said in Register Qt type as meta type:

            I think your missed what my enum really is. It's not mine enum, its Qt enum.

            You're very much correct. Q_DECLARE_METATYPE should be the thing to go to in this case, but I'm curious, why do you want this as a metatype?

            PS. You may want also to call qRegisterMetaType<QtMsgType>() to get the type registered, in addition to being declared.

            Read and abide by the Qt Code of Conduct

            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