PDA

View Full Version : Push button double click



curtisw
14th February 2006, 18:51
If you have a button on a dialog that you connect to the clicked signal which has a slot that calls accept() to close the window, if you double click the button fast enough you can end up with 2 calls to accept. I know I can work around this by disabling the button in the slot and re-enabling on show, but it seems like that shouldn't be neccessary. When would you want a button click to be triggered on a window that has already been hidden?

here's a quick example. If you double click the button, you get okOn printed twice



#include <qapplication.h>
#include <qdialog.h>
#include <qpushbutton.h>
#include <iostream>

class MyDialog : public QDialog
{
Q_OBJECT
public:
MyDialog(QWidget *parent = 0)
: QDialog( parent, 0, FALSE, WDestructiveClose )
{
okBtn = new QPushButton( "&Ok", this );
connect( okBtn, SIGNAL( clicked() ), this, SLOT( onOk() ) );
}

protected slots:
void onOk()
{
std::cout << "onOk" << std::endl;
sleep(1);
accept();
}

private:
QPushButton* okBtn;
};

int main(int argc, char **argv)
{
QApplication a(argc, argv);

QDialog mainWin(0);
a.setMainWidget(&mainWin);

MyDialog *t = new MyDialog(0);
t->show();

return a.exec();

}

#include "qtDialogExample.moc"


Thanks,
Curt

michel
14th February 2006, 22:49
Sorry, but I didn't understand from the message. What exactly is your question? How to ignore the second call? Or how to specifically handle a double click?

If it is the latter you would intercept the MouseEvent and compare the event's type() with the constant QEvent::MouseButtonDblClick (probably in an if-else or switch()-case statement). If you want to know how to avoid it calling twice, maybe just have a static variable in your subclass implementation which is initialized in the class constructor, then let your new event handler only process the event if the variable is false, otherwise ignore() and pass the data on to the parent. That way the variable dies when the class is destructed and is only run once.

curtisw
15th February 2006, 12:59
My question is why would qt allow this. If the dialog has been closed and there is a buffered click action, shouldn't it just ignore that second click? When the signal is issued, the button is hidden. Working around this is easy enough. It just doesn't seem correct to issue a clicked signal from a button that is hidden.

Chicken Blood Machine
15th February 2006, 16:40
My question is why would qt allow this. If the dialog has been closed and there is a buffered click action, shouldn't it just ignore that second click? When the signal is issued, the button is hidden. Working around this is easy enough. It just doesn't seem correct to issue a clicked signal from a button that is hidden.

It wasn't hidden when you did the second click - so the behaviour is quite correct.