PDA

View Full Version : Connecting signals & slots across different threads



jyoti kumar
16th May 2006, 08:58
I have created one GUI application & one thread MyThread.
I am trying to connect signal of thread with slot of application & vice versa.
From GUI, I am calling signal connecting to Slot A of MyThread.
From the slot A of MyThread, I am emitting signal that connects slot B of application.
But my application is not running.
Pls help.

Here is ths code for connection -
CFacade *pFacade = CFacade::getUniqueInstance();
CRouterThread *pRtrThread = new CRouterThread(pFacade);

connect(CFacadeHandler::getUniqueInstance(pFacade->m_uiMainWindow), SIGNAL(readRouterInputs()), pRtrThread, SLOT(processRouterInputs()), Qt::QueuedConnection);

connect(CFacadeHandler::getUniqueInstance(pFacade->m_uiMainWindow), SIGNAL(startRouting()), pRtrThread, SLOT(runRouter()), Qt::QueuedConnection);

connect(pRtrThread, SIGNAL(displayInfo(QString)), pFacade->m_uiMainWindow.outputPane, SLOT(append(QString)), Qt::QueuedConnection);

connect(pRtrThread, SIGNAL(taskOver()), CFacadeHandler::getUniqueInstance(pFacade->m_uiMainWindow), SLOT(changeCursorShape()), Qt::QueuedConnection);

pRtrThread->start();

wysota
16th May 2006, 10:44
What do you mean by "application is not running"?

jyoti kumar
16th May 2006, 12:33
What do you mean by "application is not running"?

I meant that I was not able to execute the application.
That problem is resolved, but I am facing one more problem -
I am trying to create 2 threads from GUI application - CRouterThread & CChipViewerThread.
CRouterThread is working fine, but I am not able to instantiate another GUI application on CChipViewerThread.

Pls find attached the files for the same.
PSSController is tha main class where I am creating thread & corresponding connections.
CChipViewerThread is the thread class.

Regards

jacek
16th May 2006, 12:40
CRouterThread is working fine, but I am not able to instantiate another GUI application on CChipViewerThread.
There can be only one GUI thread in Qt application.

http://doc.trolltech.com/4.1/threads.html

jpn
16th May 2006, 12:40
See: Thread support in Qt (http://doc.trolltech.com/4.1/threads.html#qobject-reentrancy)



Although QObject is reentrant, the GUI classes, notably QWidget and all its subclasses, are not reentrant. They can only be used from the main thread. As noted earlier, QCoreApplication::exec() must also be called from that thread.

In practice, the impossibility of using GUI classes in other threads than the main thread can easily be worked around by putting time-consuming operations in a separate worker thread and displaying the results on screen in the main thread when the worker thread is finished. This is the approach used for implementing the Mandelbrot and the Blocking Fortune Client example.