PDA

View Full Version : How to disable enter



phillip_Qt
10th March 2010, 10:01
in my dialog a pushbutton (named Yes) is there.
in constructor i gave like


connect(this->yesButton,SIGNAL(clicked()),this,SLOT(accept()));

I dont want to accept this dailog if Enter key from keyboard is pressed. how to disable the focus. Only if i mouseclick on this button it should accept. How to do?

wagmare
10th March 2010, 10:11
use eventFilter on the pushButton and filter the key press
bool myclass:: eventFilter(QObject *ob, QEvent *e)
{
if(ob == pushbutton && e->type() == QEvent::KeyPress) {
xxxx
return true;
}
return QWidget::eventFilter(ob, e);
}

phillip_Qt
10th March 2010, 10:24
use eventFilter on the pushButton and filter the key press
bool myclass:: eventFilter(QObject *ob, QEvent *e)
{
if(ob == pushbutton && e->type() == QEvent::KeyPress) {
xxxx
return true;
}
return QWidget::eventFilter(ob, e);
}

Hi wagmare,
its not working. :( if i press enter key also its closeing the dialog.

wagmare
10th March 2010, 10:29
show the filter code u implemented .. did u use installEventFilter on pushButton

phillip_Qt
10th March 2010, 10:37
show the filter code u implemented .. did u use installEventFilter on pushButton

i used like

bool CDialog::eventFilter(QObject *i_Object, QEvent *i_Event)
{
if(i_Object == yesButton && i_Event->type() == QEvent::KeyPress)
{
return true;
}
else
return QWidget::eventFilter(i_Object, i_Event);
}

void CDialog::installEventFilter ( QObject * filterObj )
{
this->installEventFilter(yesButton);
}

wagmare
10th March 2010, 10:42
include this inside the if() condition
const QKeyEvent *ke = static_cast<QKeyEvent *>(e);
if(ke->key()==Qt::Key_Enter /** or use Qt::Key_Enter **/){
return false;

and use simply use
yesButton->installEventFilter(this);

wysota
10th March 2010, 10:57
I dont want to accept this dailog if Enter key from keyboard is pressed. how to disable the focus. Only if i mouseclick on this button it should accept. How to do?
Set the focus policy of the button object to NoFocus and set its 'default' and 'autoDefault' properties to false.

phillip_Qt
10th March 2010, 11:14
Set the focus policy of the button object to NoFocus and set its 'default' and 'autoDefault' properties to false.

No it didnt help. its closing the dialog. i need diloag not to be executed untill i press yesbutton.

wysota
10th March 2010, 12:36
Could you provide a minimal compilable example reproducing the problem?

phillip_Qt
10th March 2010, 13:34
Could you provide a minimal compilable example reproducing the problem?

herewith i've attched the code. if i press pushbutton, a new pop up is coming. if i press enter key from keyboard its closing the pop up. i need the pop up should be accepted if i mouse click yes button or rejected in No button.

wysota
10th March 2010, 15:10
Strange... when I change your code to do what I told you to do in my first post it... works correctly.

raidsan
23rd August 2012, 07:52
maybe you should use Qt::Key_Return instead of Qt::Key_Enter.
Qt::Key_Enter is the keypad Enter.