PDA

View Full Version : Another multithreading problem



Sheng
30th October 2008, 16:21
It seems it's quite annoying to use the multithreading features in Qt.
Here is another problem:

in class A, I have functionA(), functionA will call a static function functionB() in class B.
It seems I can not run functionA() in another thread,
the error is
QObject::setParent: New parent must be in the same thread as the previous parent

if I do not call B::functionB(), it works fine.

any ideas?

wysota
30th October 2008, 17:12
What's inside functionB()?

Sheng
30th October 2008, 18:03
What's inside functionB()?

functionB is a pretty complex function, it's a function in Controller, will deal with datamodel and low level libraries(to control usb hardware) .

Btw, it's run time error, no error during compilation.

wysota
30th October 2008, 22:10
Inside the function you surely create some QObject subclass instance passing a parent object that was created in another thread and that's the problem. I suggest you post the function so that we can discuss how to improve it.

Sheng
31st October 2008, 14:14
Inside the function you surely create some QObject subclass instance passing a parent object that was created in another thread and that's the problem. I suggest you post the function so that we can discuss how to improve it.

The only suspicious part of the function is I called a static function of QThread subclass SleeperThread(self defined) to implement sleep function. The other part is just call low level library, after get parameters, will update DataModel which emit signals to update GUI.
I solved the problem by instead of running functionA in a new thread, making some changes in functionA, directly calling functionA and generate a new thread in functionA to run B::functionB. And it works fine.