How can I inherit both QWidget and QThread?

I have a class like this

class GUI : public QWidget, public QThread 

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

thanks

+7
c ++ qt qthread
source share
3 answers

You can not. Both QWidget and QThread inherit (not virtual) from QObject . Therefore, you do not have a virtual derivation, therefore, two copies of QObject , which confuses the compiler. QObject was specifically designed this way. Cm:

There are some who allegedly circumvented this (I can’t find the link right now, but there on Google, I had the same problems two weeks ago), but in the best case it is unsafe.

Edit: The best way, probably, is to inherit another object from QThread and save that object as a member in your GUI class. This is a kind of workaround that most people make in this matter.

+11
source share

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

0
source share

You cannot inherit multiple QObjects.

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

 class GUI : public QWidget { QThread myThread; }; 

You named your class GUI - is this the main GUI of your program? See the Examples in the Qt Samples folder - they have sample GUI programs and Themes .

-one
source share

All Articles