Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i inherit from both QWidget and QThread?

Tags:

c++

qt

qthread

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

like image 433
infinitloop Avatar asked Apr 07 '10 20:04

infinitloop


People also ask

When to use QThread?

A QThread should be used much like a regular thread instance: prepare an object (QObject) class with all your desired functionality in it. Then create a new QThread instance, push the QObject onto it using moveToThread(QThread*) of the QObject instance and call start() on the QThread instance. That's all.

What is QThread?

A QThread object manages one thread of control within the program. QThreads begin executing in run(). By default, run() starts the event loop by calling exec() and runs a Qt event loop inside the thread. You can use worker objects by moving them to the thread using QObject::moveToThread().


1 Answers

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:

  • http://lists.trolltech.com/qt-interest/2006-10/msg00711.html
  • http://www.qtforum.org/article/23295/problem-opening-the-qfiledialog.html

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.

like image 113
laura Avatar answered Oct 09 '22 05:10

laura