PDA

View Full Version : qpushbutton not disabling properly



abrou
19th February 2008, 21:49
Hello, I am using Qt 4.3.3 with visual studio express 2005. I want to disable a pushbutton while the program is doing something, so I tried this:


connect( searchButton, SIGNAL( clicked() ), this, SLOT( listFiles() ) );
The behaviour I want is for the searchButton to be disable when the program is searching, so the user doesn't get impatient and press is multiple times (even though there is a progress bar, so they know it is thinking). What happens, is it isn't behaving disabled until AFTER the code has run. The qDebug replies false (correctly) at the correct time, which doesn't reflect the behaviour.


void RoboSearch::listFiles()
{
searchButton->setEnabled(false);
qDebug() << searchButton->isEnabled();
... //code is searching through files, so it takes ~5s
//searchButton->setEnabled(true);
}

I commented the enabling of the button to debug to make sure that it was eventually disabling.

Does anybody know what I'm doing wrong or what I can do to fix it? Thanks

ChristianEhrlicher
20th February 2008, 06:40
As setEnabled triggers a redraw (to paint the disabled state) and you don't give your application time to execute the event loop, Qt can't update the button.

abrou
20th February 2008, 18:25
Hmm... Alright, thanks.

abrou
22nd February 2008, 23:07
Alright, I'm having trouble solving this. How would you suggest going about this? Thanks

jpn
22nd February 2008, 23:17
The shortest possible way to solve the problem is to add a call to QCoreApplication::processEvents().


void RoboSearch::listFiles()
{
searchButton->setEnabled(false);
// let the application process its events
QApplication::processEvents(QEventLoop::ExcludeUse rInputEvents);
//code is searching through files, so it takes ~5s
searchButton->setEnabled(true);
}

5s is a long time for a GUI application to be frozen so you might want to call the same method every now and then during the search.

abrou
23rd February 2008, 00:03
Thank you very much!

I am now trying apply it to another part of my code, where a pushbutton connection opens a dialog, but the dialog won't actually open until I close the original window.... hopefully I'm on the right track.

jpn
23rd February 2008, 08:35
I am now trying apply it to another part of my code, where a pushbutton connection opens a dialog, but the dialog won't actually open until I close the original window.... hopefully I'm on the right track.
Hmm sorry, but it's hard to say anything without seeing relevant code.