How to disable a button after clicking it?
-
wrote on 7 Jun 2011, 22:55 last edited by
Hello, I would like to have a button which gets disabled as soon as it gets clicked and gets enabled again after the calculations have finished, so the user cannot click on the button several times during the computing process and crash the program.
@
void GUI::onCalculate()
{
ui->pbCalculate->setEnabled(false);
ui->pbCalculate->repaint();
//do calculations which take some time
ui->pbCalculate->setEnabled(true);
}
@This doesn't work, because even though the button looks disabled it still performs the event if I click on it again and again.
This is how I implemented the click-event(clicked() and released() don't solve my problem either):
@connect(ui->pbCalculate, SIGNAL(pressed()), this, SLOT(onCalculate()));@Found "this topic":http://developer.qt.nokia.com/forums/viewthread/4780 but it doesn't quite answer my question, because there is a second button which should be disabled and enabled (which works fine, but that's not what I want). Also I want to avoid using threads for solving this problem.
Thanks, Ferdl
-
wrote on 7 Jun 2011, 23:58 last edited by
i would have thought that should work one approach is to disconnect the signal while you are doing your processing and reconnect it
@
void GUI::onCalculate()
{
disconnect(ui->pbCalculate, SIGNAL(pressed()), this, SLOT(onCalculate()));
ui->pbCalculate->setEnabled(false);
ui->pbCalculate->repaint();
//do calculations which take some time
ui->pbCalculate->setEnabled(true);
connect(ui->pbCalculate, SIGNAL(pressed()), this, SLOT(onCalculate()));
}
@i didn't compile or test that code but it should work.
-
wrote on 8 Jun 2011, 00:06 last edited by
Thanks for your reply, but unfortunately this doesn't work either :/
-
wrote on 8 Jun 2011, 00:12 last edited by
Use a single shot timer to delay the calculation and thus allow the UI to update.
(Untested code)
@
void GUI:onCalculate()
{
ui->calculateButton->setEnabled(false);
QTimer::singleShot(0, this, SLOT(doCalculate()));
}void GUI::doCalculate()
{
// do calculations which take some time
ui->calculateButton->setEnabled(true);
}
@ -
wrote on 8 Jun 2011, 00:28 last edited by
Same result.
I've uploaded the whole project if someone's interested in this challenge ;)"Project Euler Nr. 5":http://www.mediafire.com/?r6rkv94rxbnxavt
I know it's not that important, but I wanted to make it "uncrashable" and 100% bug-free.
-
wrote on 8 Jun 2011, 05:45 last edited by
Have you tried to perform the calculations in separate thread and to notify the GUI when they are ready?
-
wrote on 8 Jun 2011, 06:26 last edited by
Try this:
@
void GUI::onCalculate()
{
ui->pbCalculate->setEnabled(false);QApplication::processEvents();
//do calculations which take some time
ui->pbCalculate->setEnabled(true);
}
@ -
wrote on 8 Jun 2011, 06:49 last edited by
I think the problem is another one. The mouse click event that triggeres the first clicked event is currently processed, so the main eventloop is blocked. inside this blocked state, you disable the button (which is no real window, just a widget). The second click event is emitted by mouse to the OS and the OS sends the event to the message loop of the Qt application. It will be send to the widget, when the event loop is entered again, which happens after GUI::onCalculate() is left.
So during processing of the event, the widget is not disabled...
long calculations should not be done inside the GUI thread so the UI is not blocked. Then this would also not happen, you could trigger some thread, threadpool, QFuture, whatever, disable the button and return. Then the mouse click during the thread is running, will not be processed by the button.
-
wrote on 8 Jun 2011, 13:54 last edited by
Okay, looks like there's no way around threads for this. Thanks for the advises and explanation.
I'll try it out and report back if I succeed.PS: I forgot to mention that the calculations aren't performed within the GUI class/(thread?).
The whole method looks like this:@
void GUI::onCalculate()
{
ui->pbCalculate->setEnabled(false);
ui->lwOutput->clear();
ui->pbCalculate->repaint();
ui->lwOutput->repaint();Euler euler; int j, k, n; n=ui->sbInput->text().toInt(); for(k=1; k<=n; k++) { j=euler.calculate(k); ui->lwOutput->addItem(QString::number(k) + ": " + QString::number(j)); ui->lwOutput->doItemsLayout(); ui->lwOutput->repaint(); } ui->pbCalculate->setEnabled(true);
}
@ -
wrote on 8 Jun 2011, 14:57 last edited by
The calculations are performed in the main thread in the code above. A class does not equal a thread.
-
wrote on 8 Jun 2011, 15:17 last edited by
[quote author="Andre" date="1307545054"]The calculations are performed in the main thread in the code above. A class does not equal a thread. [/quote]
Thanks for clarifying this, I wasn't sure about this hence the question mark.
-
wrote on 8 Jun 2011, 18:20 last edited by
This piece of code did the trick for me:
@
GUI::GUI(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::GUI)
{
ui->setupUi(this);
euler = new Euler();connect(ui->pbCalculate, SIGNAL(clicked()), this, SLOT(onCalculate())); connect(ui->actionExit, SIGNAL(triggered()), qApp, SLOT(quit())); connect(euler, SIGNAL(finished()), this, SLOT(calculationsDone()));
}
void GUI::onCalculate()
{
ui->pbCalculate->setEnabled(false);
ui->pbCalculate->repaint();
ui->lwOutput->clear();
ui->lwOutput->repaint();int n; n=ui->sbInput->text().toInt(); euler->setN(n); euler->start(QThread::IdlePriority);
}
void GUI::calculationsDone()
{
int i;
QList<int> results = euler->getResults();for(i=0; i<results.size(); i++) { ui->lwOutput->addItem("Number " + QString::number(results[i]) + " is the smallest number which can be divided evenly by all numbers from 1 to " + QString::number(i+1)); } ui->lwOutput->doItemsLayout(); ui->lwOutput->repaint(); ui->pbCalculate->setEnabled(true); ui->pbCalculate->repaint();
}
@ -
wrote on 15 Aug 2011, 17:42 last edited by
Actually, this worked perfectly for me:
@void wave::on_pushButton_clicked() //Starts program
{
thread->start();
ui->pushButton->setEnabled(false);
ui->pushButton_2->setEnabled(true);
}void wave::on_pushButton_2_clicked() //Terminates program
{
thread->terminate();
ui->pushButton_2->setEnabled(false);
ui->pushButton->setEnabled(true);}@
-
wrote on 16 Aug 2011, 16:01 last edited by
See http://developer.qt.nokia.com/wiki/Threads_Events_QObjects , especially the last examples. If your job is not splittable and/or you can't insert calls to QCoreApplication::processEvents, use a separate thread.