user3443063
user3443063

Reputation: 1623

Qt c++ class+subclass using Q_OBJECT doesn´t work

I`m having the hardest time in creating a class+subclass using Q_OBJECT:

I have a class geometry and some classes Geo_1PF, Geo_2,.. that inherit from geometry. I created those classes an everything worked well. Now I want to use QMetaType and for that I need to declare Q_Object. Once I used Q_OBJECT / Q_DECLARE_METATYPE my problems started.

I get the error "undefined reference to vtable for ..." I tried to delete all .o and all moc_.cpp before compiling again. I added QT += core in the *.pro-File but I didn`t succeed.

Can somebody help me please?

This is my code:

geometry.h:

    #ifndef GEOMETRY_H
    #define GEOMETRY_H
    #include <QMetaType>        
    #include <QWidget>
    #include <QObject>   
    #include <QDebug>        

    class Geometry
    {
       // Q_OBJECT
    protected:

    public:
        Geometry();
        virtual ~Geometry(void) {}

        virtual void write_LNE(); 

    //Q_DECLARE_METATYPE(Geometry);
    #endif // GEOMETRY_H

-

geometry.cpp:

#include "geometry.h"

    Geometry::Geometry()
    { qDebug() << "Constructor:  hier ist Geometry";        }


    void Geometry::Haupt()
    {  qDebug() << " Das hier ist die Haupt von Geometry ....." ; }      
    void Geometry::write_LNE(){}

-

 Geo_1PF.h:

    #ifndef GEO_1PF_H
    #define GEO_1PF_H
    #include "geometry.h"

    class Geo_1PF : public Geometry
    {
       // Q_OBJECT
    public:
        Geo_1PF();
        ~Geo_1PF() {}

        virtual void write_LNE();        
    };

    //Q_DECLARE_METATYPE(Geo_1PF);
    #endif // GEO_1PF_H

Upvotes: 0

Views: 1512

Answers (2)

dtech
dtech

Reputation: 49329

First, the Q_OBJECT macro is not inheritable. It must be included in every object that directly or indirectly inherits QObject. If your base class is QObject derived, then your derived class is also QObject derived, and thus must include the macro.

From the documentation:

The Q_OBJECT macro must appear in the private section of a class definition that declares its own signals and slots or that uses other services provided by Qt's meta-object system.

Second, Q_DECLARE_METATYPE() requires that the object has a copy constructor, and QObject doesn't support copy constructors by design, so it is impossible to use Q_DECLARE_METATYPE() with a QObject derived class. And it doesn't make sense either, because every QObject is automatically a meta type, since it is being processed by the meta object compiler. It will work if you declare a pointer to your type - Q_DECLARE_METATYPE(Geometry*). Registering a type as a meta type requires the type to have a copy constructor, if it doesn't you get an error.

From the looks of your classes, they don't need to derive from QObject, so you should not do that, you should remove the macro (and never use it if you are not inheriting a QObject or derived), and then Q_DECLARE_METATYPE(Geometry) should work. It looks like you were under the impression you need Q_OBJECT to Q_DECLARE_METATYPE() but in reality it is the opposite - the two are incompatible - the latter must have and the former must not have a copy constructor.

Upvotes: 2

Vladimir Bershov
Vladimir Bershov

Reputation: 2831

If you want to use Q_OBJECT your class should inherites from QObject or its subclasses

class Geometry : public QObject
{
    Q_OBJECT

//...

See Using the Meta-Object Compiler (moc)

Upvotes: 6

Related Questions