PDA

View Full Version : problem try to trigger a refresh of my Ui (the main thread) from an external QThread



eric_vi
5th November 2010, 17:30
Hello,

i have my main thread (GUI thread) and i have launched a new thread and established a connection between this new thread (send a signal) and the GUI thread (receive message in slot).

I am trying in the slot of this connection to refresh my UI. For so i do ... qApp->processEvents().

I can't get that to refresh my UI... only if i do qApp->processEvents() from my GUI thread, i can get the refresh.

what do i do wrong or what should i do?

Thanks for your help

Lesiok
5th November 2010, 17:32
Please show how did You create connection.

tbscope
5th November 2010, 17:33
I'm sure your connections are not correct. Can you post the code please?

And why do you need to call qApp->processEvents()?
Certainly don't do that from another thread.

eric_vi
5th November 2010, 18:16
thank you for your fast answer here is the code in question,
i use qApp->processEvents ( QEventLoop::ExcludeUserInputEvents ) because usually that process my queue



from my main GUI thread here is my slot



void MainWindow::updateGUI_slot()
{

qApp->processEvents ( QEventLoop::ExcludeUserInputEvents );
qApp->flush();

}








here is my timerThread, (the one that has been created from the main thread),
so _parent is my MainWindow pointer



timerThread::timerThread (QObject *_parent)
{
parent = _parent;
}

timerThread::~timerThread ()
{
delete timer;
}



void timerThread::run ()
{
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), parent, SLOT(updateGUI_slot()), Qt::DirectConnection );
timer->start(1000);

exec();

}

Lesiok
5th November 2010, 21:04
Problem is with Qt::DirectConnection. You can't "play" with GUI thread from another thread. Qt::DirectConnection is like ordinary calling of method. Change this connection to Qt::QueuedConnection (it is default for interthreads signals) and all will be working.

eric_vi
5th November 2010, 21:24
thanks again,

well i tried nearly all QueuedConnection and AutoConnection, i have the same behaviour and still not getting the refresh of my UI except if i do it from my main thread so i guess i still have
updateGUI_slot() in my timerThread don't see why!






void timerThread::run ()
{
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), parent, SLOT(updateGUI_slot()), Qt::QueuedConnection );
timer->start(1000);

exec();

}

Lesiok
6th November 2010, 07:54
If You really need call processEvents() to refresh GUI it is something wrong with logic of application. Show us authentic code.
P.S.
Create connection in auto mode and add this line to timerThread constructor :
moveToThread( this);

tbscope
6th November 2010, 08:32
add this line to timerThread constructor :
moveToThread( this);

That's not correct.
Instead move the code of the timerThread to a QObject subclass. Then move that object to a thread.