PDA

View Full Version : I'm failing with Threading



thomaspu
11th January 2008, 17:27
I've got a widget that I want to have operate inside its own thread, SWidget. I've got another class called SWidgetThread that inherits QThread. It declares a new SWidget and then fires off exec() inside its run() function so that that thread has its own event loop.

the problem that I'm running into is that I want a TabWidget to have each tab (made up of a SWidget) run in a seperate thread. So I was trying to have my TabWidget create a new SWidgetThread object, and then I ask my SWidgetThread object for a pointer to its SWidget and that is the widget that I give to the TabWidget as its page. But later when I go to get rid of the tab, I get a pointer to the page, but that page's->thread object is the same as the TabWidget's->thread. So when I call quit() on my page, it kills the main thread.

Help?
Paul

marcel
11th January 2008, 18:18
Only objects created in the run method live in the worker thread.
The widget lives in the GUI thread(as it should be!!!) because you create it in the QThread constructor.

Anyhow, this design doesn't sound good at all. First of all widgets MUST not be modified directly by other threads than the GUI thread(I am not sure if you respect that), instead you should use signals and slots/events.

Second, this widget-thread association doesn't have to be like that. You could do it in countless other ways, such as maps, a widget/thread pair class, etc..

thomaspu
11th January 2008, 20:40
Ok, that clears things up. I think I'm doing things really wrong then and need to rethink my design ;p

Paul