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. Registering QMetaType not working
Qt 6.11 is out! See what's new in the release blog

Registering QMetaType not working

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 4.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.
  • G Offline
    G Offline
    goocreations
    wrote on last edited by
    #1

    I'm using Qt 5.0.0 and have problems registering my own class as a QMetaType. I'm not sure if this is a bug, since they've change a lot in QMetaType. I have the following structure:

    @class Base : public QObject, public OtherClass
    {
    Q_OBJECT

    public:
        Base();
        Base(const Base &other);
        virtual ~Base();
    

    };

    class Subclass : public Base
    {
    Q_OBJECT

    public:
        Subclass();
        Subclass(const Subclass &other)
        ~Subclass();
    

    };

    Q_DECLARE_METATYPE(Subclass);@

    Whenever I try to retrieve the type with QMetaType::type("Subclass"), it returns 0 (MetaType::UnknownType). Does anyone have an idea what I'm doing wrong? Or is this a bug?

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

      Hi,

      You can't have a QObject as meta type because copying QObject is forbidden (in fact, the copy constructor is disabled).
      You might get away using a pointer meta type.

      Do your classes really need to be QObjects ?

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

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goocreations
        wrote on last edited by
        #3

        No, originally I didn't inherit from QObject. But it didn't work, so I thought that QObject might fix the problem. So even with this structure, it is not working (OtherClass is a simple class inheriting from nothing):

        @ class Base : public OtherClass
        {

            public:
                Base();
                Base(const Base &other);
                virtual ~Base();
        };
         
        class Subclass : public Base
        {
           
            public:
                Subclass();
                Subclass(const Subclass &other)
                ~Subclass();
        };
         
        Q_DECLARE_METATYPE(Subclass);
        

        @

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

          In that case, did you a

          @qRegisterMetaType<Subclass>();@

          somewhere ?

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

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goocreations
            wrote on last edited by
            #5

            That always worked in Qt4, but in Qt5 I get the following error:

            @error: specializing member ‘::qRegisterMetaType<Subclass>’ requires ‘template<>’ syntax@

            qRegisterMetaType<Subclass>("Subclass") also doesn't work

            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