PDA

View Full Version : Return key in an editable QComboBox accepts a QDialog



vfernandez
3rd June 2006, 03:16
I have an editable QComboBox inside a QDialog. When I type some text in the QComboBox and I press return, I would like my dialog to do some things. So I've tried to connect the returnPressed() signal from QComboBox::lineEdit() to my slot, but when I press return, the dialog is accepted. That should be the normal behaviour when the focus is in other widgets and the user presses return, but not in this widget. Does someone know how to prevent the QDialog from being closed in this case?

jpn
3rd June 2006, 05:24
QComboBox and the QLineEdit inside both ignore key press events by default. This means that these events will be propagated to the parent widget, the dialog in your case.

One workaround is to subclass QComboBox and override keyPressEvent() to change the behaviour. You should get the returnPressed() signal in the normal way. The only difference is that the event won't be delivered to the dialog. The code could look more or less like this:


void MyComboBox::keyPressEvent(QKeyEvent* e)
{
// let base class handle the event
QComboBox::keyPressEvent(e);

if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return)
{
// accept enter/return events so they won't
// be ever propagated to the parent dialog..
e->accept();
}
}

kghose
10th July 2008, 15:56
Just to say Thanks.

Couldn't get the thanks button to work :(