PDA

View Full Version : Threads - General issue!



deepakn
24th August 2009, 08:30
Hello all,
I have an application with separate .ui file for the GUI.
Now the UI has a statistics page with few tables which needs to update some data periodically. Data is fetched from backend[written in C] using a simple [update]function. This function involves certain manipulations on UI elements(ex: ui.trafficStatsTable->setItem(row, 1, resourceItem2); ).

Now i want to update the table repeatedly in a separate thread. I havent used threads before.
I tried pthreads and am not able to implement the required function as void*

Then I went for QThread. Again, faced several problems. I was not able to access the UI elements.

I found this in our documentation.

In GUI applications, the main thread is also called the GUI thread because it's the only thread that is allowed to perform GUI-related operations. Could someone please explain what this means? Does it mean that simple operations like filling a table with data cannot be done in a different thread?

Could someone provide some help please? Any pointers to some examples involving threads where UI is done separately would be of much help.

Thanks in advance.
Deepak

nish
24th August 2009, 08:37
you can not directly acces the gui elements in seperate thread...

but you can use singals and slots that will update the gui... in other words... in your thread class make a signal like

void addTextToTable(QString text);

and then emit it whenever you want to change the text in gui...
in the main gui thread... create a slot and connect it to the above signal..
thats it.

deepakn
24th August 2009, 08:44
Thank you MrDeath, that was exactly some guidance i was searching for. I will try it now and post back the results. :)

victor.fernandez
24th August 2009, 13:01
Take a look at the Queued Custom Type example (http://qt.nokia.com/doc/4.5/threads-queuedcustomtype.html). It will show you how can you create a worker thread and send custom data to the GUI using signals/slots across threads (http://qt.nokia.com/doc/4.5/threads.html#signals-and-slots-across-threads).

deepakn
31st August 2009, 13:05
Queued Custom Type Example was of much help. :) thanks a lot victor.
Now I am stuck with another issue, and I think its again a silly one. Still gotta find a solution!

Here is my code:
master.cpp


thread = new RenderThread();
connect(ui.updateStatsButton, SIGNAL(clicked()), this, SLOT(updateStats()));
....

//in updateStats()
thread->startOffThread(sessionName, logPos, event_id);


thread.cpp

void RenderThread::startOffThread(const QString &str, int logPos, Event_Id event_id)
{
...
//some manipulations on variables supplied
start();
}

//in RenderThread::run()
do {
...
emit sendString(str, str2);
sleep(1);
}while (some_condition);


Now here is my problem - I have multiple Events on which the above actions have to be performed independently.
suppose i have Event01 and Event02. I clicked updateStats button for Event01 and code is executed properly and stats are being updated. Meanwhile I clicked updateStats for Event02 (where in functioncall
thread->startOffThread(sessionName, logPos, event_id);the variables supplied differ) Event01 updation stops and Event02 starts!
:(

I want Event01 and Event02 (and many many more) to run parallelly without affecting each other.

How do I implement multiple threads?
How do start and stop them? I could not find anything similar to thread_id from pthreads.
someone please help...

wysota
31st August 2009, 13:17
Read about QtConcurrent::run()