Results 1 to 7 of 7

Thread: qpushbutton not disabling properly

  1. #1
    Join Date
    Jan 2008
    Location
    Vancouver, Canada
    Posts
    54
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default qpushbutton not disabling properly

    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:

    Qt Code:
    1. connect( searchButton, SIGNAL( clicked() ), this, SLOT( listFiles() ) );
    To copy to clipboard, switch view to plain text mode 
    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.

    Qt Code:
    1. void RoboSearch::listFiles()
    2. {
    3. searchButton->setEnabled(false);
    4. qDebug() << searchButton->isEnabled();
    5. ... //code is searching through files, so it takes ~5s
    6. //searchButton->setEnabled(true);
    7. }
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qpushbutton not disabling properly

    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.

  3. #3
    Join Date
    Jan 2008
    Location
    Vancouver, Canada
    Posts
    54
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qpushbutton not disabling properly

    Hmm... Alright, thanks.

  4. #4
    Join Date
    Jan 2008
    Location
    Vancouver, Canada
    Posts
    54
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qpushbutton not disabling properly

    Alright, I'm having trouble solving this. How would you suggest going about this? Thanks

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: qpushbutton not disabling properly

    The shortest possible way to solve the problem is to add a call to QCoreApplication::processEvents().
    Qt Code:
    1. void RoboSearch::listFiles()
    2. {
    3. searchButton->setEnabled(false);
    4. // let the application process its events
    5. QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
    6. //code is searching through files, so it takes ~5s
    7. searchButton->setEnabled(true);
    8. }
    To copy to clipboard, switch view to plain text mode 
    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.
    J-P Nurmi

  6. The following user says thank you to jpn for this useful post:

    abrou (23rd February 2008)

  7. #6
    Join Date
    Jan 2008
    Location
    Vancouver, Canada
    Posts
    54
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qpushbutton not disabling properly

    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.

  8. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: qpushbutton not disabling properly

    Quote Originally Posted by abrou View Post
    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.
    J-P Nurmi

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.