开发者

how can i inherit from both QWidget and QThread?

I have a class like this

class GUI : public QWidget开发者_JS百科, 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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜