Registering QMetaType not working



  • 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?


  • Lifetime Qt Champion

    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 ?



  • 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);
    

    @


  • Lifetime Qt Champion

    In that case, did you a

    @qRegisterMetaType<Subclass>();@

    somewhere ?



  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.