Results 1 to 3 of 3

Thread: Disable Buttons during long operation.

  1. #1
    Join Date
    Jun 2012
    Posts
    219
    Thanks
    28
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Disable Buttons during long operation.

    Saving a .png takes a long time on the humble hardware. While it's being saved, I don't want the buttons to accept clicks.

    I do QPushbutton::setEnabled(false) on them. They go grey, but if I click on them the click is accepted and processed after the long operation completes.

    Is this expected behavior? Do I need to disconnect the signals and reconnect or is there a better way to disable clicks during a long operation?

    It's Ok to block the GUI, that's what I want. I don't want the user to do anything until the operation completes.

    (The windowModality is ApplicationModal and a ProgressBar is displayed also.)

    Thanks,

    Dave Thomas

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Disable Buttons during long operation.

    I do QPushbutton::setEnabled(false) on them.
    That's how you do it.
    They go grey, but if I click on them the click is accepted and processed after the long operation completes.
    The button simply cannot be clicked and cannot execute any slot attached to it when not enabled. Something else is going on here.

    Try this small example. Click the button multiple times while the process is running and note it does not start another run.
    Qt Code:
    1. #include <QApplication>
    2. #include <QPushButton>
    3. #include <QTimer>
    4. #include <QEventLoop>
    5.  
    6. class Button: public QPushButton
    7. {
    8. Q_OBJECT
    9. public:
    10. Button(QWidget *p = 0): QPushButton(p) {
    11. setText("Push to Start");
    12. connect(this, SIGNAL(clicked()), SLOT(start()));
    13. }
    14. public slots:
    15. void start() {
    16. static int count = 1;
    17. setEnabled(false);
    18. setText(QString("Run number %0 ...").arg(count++));
    19.  
    20. // A fake 5 second long task that keeps the UI alive.
    21. QEventLoop loop;
    22. for (int i = 0; i < 50; ++i) {
    23. QTimer::singleShot(100, &loop, SLOT(quit()));
    24. loop.exec();
    25. qApp->processEvents();
    26. }
    27.  
    28. setText("Push to Start");
    29. setEnabled(true);
    30. }
    31. };
    32.  
    33. int main(int argc, char **argv)
    34. {
    35. QApplication app(argc, argv);
    36. Button b;
    37. b.show();
    38. return app.exec();
    39. }
    40. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 


    Afterthought: If you have connected the same button more than once then the first click will execute the slot more than once. Additional clicks while the button is disabled will not add to this.
    Last edited by ChrisW67; 11th August 2014 at 04:01. Reason: Added afterthought

  3. #3
    Join Date
    Jun 2012
    Posts
    219
    Thanks
    28
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Disable Buttons during long operation.

    I'll try the example, but it sure seems like a click on a pushbutton when it's disabled is "remembered" and executes the associated slot when the button is enabled.

    Here's the code fragment:

    Qt Code:
    1. QString filename = crn + "_" + dogName + "_" + QString::number(imageSuffix) + ".png";
    2. filename.replace(QRegExp("[<>:\"/\\|?*]"), "");
    3.  
    4. pb->setWindowModality(Qt::ApplicationModal);
    5. pb->setVisible(true);
    6. //pb->show() ;
    7. ui->Accept->setEnabled(false);
    8. ui->Freeze->setEnabled(false);
    9. ui->Reject->setEnabled(false);
    10.  
    11. QApplication::processEvents();
    12.  
    13. bool success = stillImage.save(imageDirectory + filename) ;
    14.  
    15.  
    16.  
    17. if (!success )
    18. {
    19. qDebug() << "something went wrong";
    20. }
    21. qDebug() << imageDirectory + filename;
    22. qDebug() << crn;
    23. qDebug()<< filename;
    24.  
    25. QSqlQuery qry;
    26. qry.prepare("INSERT INTO title_pictures (crn,file_name) "
    27. "VALUES( :crn, :file_name);" );
    28.  
    29. qry.bindValue(":crn", crn);
    30. qry.bindValue(":file_name", filename);
    31.  
    32. int rows_affected = SqlUtils::exec_query(qry,false,false);
    33. qDebug() << rows_affected;
    34.  
    35. // Save the image here.
    36. settings.setValue("image_suffix",imageSuffix++) ;
    37.  
    38. emit startCapture(true);
    39.  
    40. pb->setVisible(false);
    41. //pb->hide() ;
    42. ui->Accept->setEnabled(true);
    43. ui->Freeze->setEnabled(true);
    44. ui->Reject->setEnabled(true);
    To copy to clipboard, switch view to plain text mode 

    pb is a QProgressBar *. The long operation is the stillImage.save() (saving a QPixmap). If I click on buttons during the five second save (they are greyed out), the slot code gets executed.

    Platform is raspbian.

    Dave Thomas

Similar Threads

  1. Replies: 0
    Last Post: 26th September 2011, 23:14
  2. design pattern for long time operation
    By Talei in forum Qt Programming
    Replies: 6
    Last Post: 22nd July 2011, 10:58
  3. Long operation -> Refresh user interface
    By pl01 in forum Qt Programming
    Replies: 2
    Last Post: 31st January 2011, 10:23
  4. Disable the minimize, maximize,close buttons
    By qtlinuxnewbie in forum Newbie
    Replies: 10
    Last Post: 25th February 2010, 10:18
  5. QFileDialog disable buttons
    By Qt Coder in forum Qt Programming
    Replies: 3
    Last Post: 11th August 2009, 11:02

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.