PDA

View Full Version : Non GUI threads



raghavendraningoji
21st July 2011, 08:01
Hi,
I wanted to pop-up a message box from pthread



//my main process
{
//calling a function say f1();
}

//in f1() I am creating a pthread
{
if (0!=pthread_create(&AnsiCommon.BALthread,NULL, Dys_BalThread,NULL))
{
qDebug()<<"draw bal thread";
//fprintf(AnsiCommon.fpLogPI,"ECG Thread error\n");
}
}

//my thread function Dys_BalThread doing some calculations:After satisfying some conditins i wanted to pop-up a message box

can anybody help me.....?

mcosta
21st July 2011, 08:20
Why are you using pthread directly and not Thread related Qt classes?

raghavendraningoji
21st July 2011, 08:35
I am basically working on Linux platform.And it more concerned about hardware.In my pthread i am reading data from a serial port . Meanwhile at the end we have to port my application to a small embedded environment. Thats why I thought of preferring pthread rather than qtclases.I hope you understand my point.

high_flyer
21st July 2011, 15:33
I hope you understand my point.
I don't.
Your concerns with hardware are irrelevant, if you have chosen to use Qt, and if you do, then use QThread.
Linking to Qt but not using some classes makes no sense, and hardware plays no role for that.
If you are not using Qt, why do you post under Qt Programming?

Also, you only stated what you wanted to do.
But what is the question?

raghavendraningoji
22nd July 2011, 05:53
Ok..Ok...I can uderstand your saying.But i just wanted to do it from pthread only by using metaobjectsystem.(queuedconnections,postevents)
plz help me.

Santosh Reddy
22nd July 2011, 08:10
...i wanted to pop-up a message box...
I hope you are referring to the use of QMessageBox

You can always use any native programming interfaces in your program, but when you want to use certain features of the a framework, you need to fit into the requirements of the framework. (I also hope you understand that when you create a thread using QThread or pthread, both will eventually create a native thread instance, so I don't understand your concern)

It should be possible to trigger (indirectly, not directly), a QMessageBox from a thread started using pthread, but when you use pthread interface to create thread, the thread will not have meta object information of itself, so I don't see a way to emit signals (queued / direct) from a thread created using native interface. Try searching for "emit signals from native thread"

(I also hope you understand the difference between signal and event)

I believe one could send an event to GUI thread, and have a event handler in the GUI thread receive the event and do what so ever wanted

I never personally tried this, though I had an opportunity to do so (because I din't want to complicate my program, and is harder to maintain), you can try this

//somewhere in your thread (so called non-gui thread)
1. When ready to pop up a message box, create a custom event (this has to be QEvent based object), say (popupEvent), on heap
2. Get handle for the guiObject
3. then post the event to the object (QApplication:: postEvent(guiObject, popupEvent)), this is static call so no need of application instance. (but it is assumed that you do have a instance of application created)

//somewhere in GUI thead
1. Create a QObject based class, and have a object of it (say guiObject)
2. Implement a event handler in this class, which will handle a the custom event to poop-up a message box
3. Then you are done, just create and show a QMessage when the popupEvent is received, with the content / message sent in popupEvent

raghavendraningoji
22nd July 2011, 09:13
Thank you very much.Let me try this.

nightghost
22nd July 2011, 14:43
I think its easier to use signals and slots (via Queued Connection) (http://doc.trolltech.com/4.7/threads-qobject.html) to communicate between 2 threads. but perhaps (just a guess) you need for both solutions a custom event handling if pthreads are used (QEventLoop)