tags:

views:

309

answers:

2

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

+3  A: 

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.

laura
A: 

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.

Will
Only the main thread can operate on widgets, so making a widget a member of a thread seems odd. What might this accomplish?
Kaleb Pederson
oops, didn't know that.
Will
"you should be inheriting from QMainWindow" is false, you should only do that if you actually want some of the features provided by QMainWindow. Any QWidget without a parent is automatically a window.
Intransigent Parsnip