Qobject Multiple Inheritance

QObject Multiple Inheritance

Unfortunately inheriting QObject twice will cause problems in moc.

From http://qt-project.org:

If you are using multiple inheritance, moc assumes that the first
inherited class is a subclass of QObject
. Also, be sure that only the
first inherited class is a QObject
.

I would suggest using something more like the delegate pattern, or recreate with a HasA not a IsA relationship.

Multiple inheritance of QObject

Make it:

class MyDialog : public QDialog
{
Q_OBJECT

public:
MyListener& listener() { return m_listener; }

private:
MyListener m_listener;
};

Multiple inheritance with qobject base

QObject is not designed for multiple inheritance. QObject doesn't support multiple inheritance from another QObjects. If you inherit from two Classes only the first one can be QObject and second not as per http://qt-project.org/doc/qt-4.8/moc.html

Virtual inheritance with QObject is not supported.

You can make association between two QObjects and forward signals between them.

You can abstract your common functionalities in a way that doesn't require a signal/slot and don't inherit that from QObject. and then inherit from it. and then mix that QObject free class in MI with your your class. You can forward calls to those inherited methods through signals/slots from Derived QObject

Why QObject needs to be the first in case of multiple inheritance

Assume that we have a class Test declared as:

class Test : public Foo, public QObject
{
Q_OBJECT
[..]
};

If you take a look at the moc_test.cpp file that the moc tool has generated, you will see something like:

[..]
const QMetaObject Command::staticMetaObject = {
{ &Foo::staticMetaObject, qt_meta_stringdata_Command,
qt_meta_data_Command, &staticMetaObjectExtraData }
};
[..]

Compiler will complain about staticMetaObject not being the member of Foo, as Foo is not a QObject. For some reason the moc tool generates this code taking the first parent class. Thus if you declare Test as:

class Test : public QObject, public Foo {};

The generated code will look fine to compiler.

I think this is made just for convenience because moc tool will hardly know which of the parent classes is a QObject without parsing the whole hierarchy.

Note: If you don't use the Q_OBJECT macro, you can derive your class from others in any order.

Multiple inheritance with QObject

`Undefined reference to `vtable for...'` 

is usually a sign of unimplemented virtual function.
Make sure you have implemented (defined) the corresponding virtual functions you inherited from the base classes.

For example this will give you the same error because the print method in B is not implemented.

class A {
public:
virtual void print() = 0;
};

class B : public A{
public:
void print();
};

int main()
{
B b;
}

Qt: Inherit QObject in parent and other QWidget subclass in child

Multiple inheritance from QObject will not work for several reasons.
(Moc-Compiler does not work correctly and QObject has member variables which makes mutliple inheritance unsafe).

You better work with composition instead of inheritance in case you want to provide the same behavious to several objects.

Closest solution to multiple inheritance through QObject subclasses

If you really need to expose QObject member functions through your A and B classes create an abstract base class (i.e. a class with only pure virtual member functions), say AbstractQObject, and re-declare there the QObject member functions you need to expose.

Have A and B derive virtually from AbstractQObject and X from QDialog, A and B.

This should solve the problem you described, but I suspect you would be better off redesigning your code.

Qt Multiple-inheritance goes wrong

Edit : As suggested below in comments, this answer is based on faulty assumptions. Please see the better answer below.

First, you have an issue of diamond inheritance. Your error is because MyTableWidget has an undefined pure virtual member function.

Frankly though, I'm not sure why you want to use multiple inheritance at all here. If it's to save on code duplication, why can't MyTreeWidget and MyTableWidget share behavioural elements via composition instead of inheritence? Is this definitely a case of is-a vs has-a? If it's code specific to widgets that is shared but don't overlap in any way with the QTableWidget/QTreeWidget approach, just write an adaptor class that will be filled with either a Tree or Table widget.



Related Topics



Leave a reply



Submit