PDA

View Full Version : Using QThread



FasTTo
6th September 2007, 19:03
Hi there,

I need some help on including threads on some widget I have done some weeks ago.

I have a module that build reports over some database data. When I first build it, I just wanted to get one report at a time and so i didn't use QThread.

Now, I want to run more than one report at a time. My problem is that I can't figure out how to use threads on my already built module.

I have a module called ReportViewWidget that runs one report at a time and updates its GUI with a result preview. Once the report execution time can sometimes be of 10 or more minutes, I want to be able to run another report while the first still running.

Can I include ReportViewWidget class in a myThread class and include myThread class into ReportViewWidget at the same time?

Can anyone help me with some idea?


Thanks in advance.

marcel
6th September 2007, 19:11
First you must understand that widgets cannot be used/modified directly in worker threads. Only the GUI thread can do it.

One way to do it is to start a thread that creates a report in the moment you need one. If you need more reports then start more threads. For example, if you need 6 reports, then you could start 3 threads; 2 reports for each thread.
If a thread finishes earlier, then it could take the task off some other thread that's still at the first report. This assumes that you have the tasks in a static data structure accessible by all threads. They should all query this data structure when they have nothing to do to see if something is available. If it is not, then you should delete them.

However, I don't know how your code looks like, but I think you mixed the report generation logic with the view logic. You should separate them.

The view module should run in the GUI thread, while the other by the worker threads.

I guess this is one solution.

Regards

FasTTo
7th September 2007, 12:16
Yes, you are right marcel. When I first start to write this module I was really noob to QT. My programming method wasn't helpfull either. I mixed a little the presentation with the logic and computation one. I have to solve that first in order to correctly use threads on the report generation.

I Like your idea about keeping track of unused/finished threads. But I think that at least for now, the application will be used to allow only 3 (maximum 4) to be running at the same time.


Thanks marcel. If you or somebody else have anything else to say regarding this thread, please do. It's very nice to read from experienced people like some of you guys.