Nyaruko
Nyaruko

Reputation: 4479

Qt: inheritance cause ambiguous

I have the following case:

class A: public QObject

class B: public A, public QThread

Then the inheritance ambiguous happens because the QObject is inherited twice... Is there a solution to this?

Upvotes: 3

Views: 1416

Answers (3)

Gabor Angyal
Gabor Angyal

Reputation: 2255

A little tricky, but might work for you:

template<class T>
class A: public T

class B: public A<QThread>

And if you need to use A just by itself then:

A<QObject> *a = new A<QObject>;

This pattern is called Mixin.

UPDATE

Ok, I realised that the moc system would obviously not work with this solution. I support hyde's answer instead.

Upvotes: 1

hyde
hyde

Reputation: 62797

Multiple inheritance of QObject base classes does not work.

You can do something like this to solve this:

class A: public QObject

class B: public A
{
    Q_OBJECT
public:
    //...
    QThread *threadController() { return &mThreadController; }

private:
    //...
    QThread mThreadController;

}

You could also write delegates for the signals and slots you need instead of exposing the whole QThread object, or just write higher level API for class B and leave its internal QThread completely hidden. Depends on what you are trying to do, really.

If you really need to subclass QThread, then just use that as member variable type instead.

Upvotes: 2

vsoftco
vsoftco

Reputation: 56557

QThread inherits non-virtually from QObject. Therefore, there is no way of inheriting down an hierarchy from both QThread and QObject without creating an ambiguity. Virtual inheritance won't help here, since you are not dealing with any diamond inheritance pattern.

A fix is to alter your design, as @Gabor Angyal mentioned.

Related question: how can i inherit from both QWidget and QThread?

Upvotes: 5

Related Questions