Reputation: 847
I want to create a thread for a class which is Inherited by QWidget. Actually, I tried with multiple inheritance with QThread and it fails and I want to run particular member function using thread. How can I achieve this? Does anyone have any idea?
Upvotes: 0
Views: 202
Reputation: 2004
Qt classes which belong to the GUI module are not reentrant. They MUST be run from the main thread.
Upvotes: 0
Reputation: 746
You could use a wrapper class that implements the thread and calls your widget's method:
class MyWidget : public QWidget
{
[...]
void threadMethod();
};
class MyThread : public QThread
{
[...]
MyThread( MyWidget* widget )
: mWidget(widget)
{
}
void run()
{
mWidget->threadMethod();
}
MyWidget* mWidget;
};
However, you should not call any QWidget methods in "threadMethod", since the GUI and and thus the widgets belong to the "main" thread, and the QWidget methods are not thread-safe!
It would probably better to keep your widget and thread code completely separate.
Upvotes: 2
Reputation: 4184
One solution could be to use nested class in which you will pass a pointer to your normal widget class and all all methods you need from nested run method.
Upvotes: 0