Question

I have a class like this

class GUI : public QWidget, public QThread

When I do the above i get errors about connect signals. The error says Reference to "connect" is ambiguous. Is there a way to inherit from both?

Thank you

Was it helpful?

Solution

You can't. Both QWidget and QThread inherit (non-virtually) from QObject. You therefore do not have virtual derivation, thus two copies of QObject, which confuses the compiler. QObject was specifically designed this way. See:

There are some who allegedly went around this (can't find the link right now, but it's out there on Google, I had the same trouble two weeks ago), but it is unsafe at best.

Edit: the best way would probably be to have another object inherit from QThread and keep that object as a member in your GUI class. That is the sort of workaround most people do in this matter.

OTHER TIPS

QWidgets and GUI related objects con not be in different threads than the application main thread. You should not inherit both of them. You can not also call the moveToThread() function of a widget.

You cannot inherit from multiple QObjects.

You can inherit from one and make the other a member variable and work from there.

class GUI : public QWidget 
{
  QThread myThread;
};

You've named your class GUI - is this the main GUI of your program? See the examples in the Qt examples folder - they have sample programs on both GUI's and Threads.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top