Preventing Enter key from triggering OK in QButtonBox in particular QLineEdit QButtonBox
I have a Dialog which has some widgets such as QComboBox
, QSpinBo开发者_开发知识库x
and some QLineEdit
. At the bottom of the widget, I have a QButtonBox
. Clicking enter in any one of the widgets will trigger the accepted()
slot for the QButtonBox
. However, I'd like to disable this automated action in one of the QLineEdits
. So, pressing Enter
key in one of the QLineEdit
wouldn't trigger the accepted()
slot for the QButtonBox
. How can I do that?
I suggest you to install an EventFilter. Something like:
bool FilterObject::eventFilter(QObject *object, QEvent *event)
{
if (object == target && event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if (keyEvent->key() == Qt::Key_Enter) {
// Special key handling
return true;
} else
return false;
}
return false;
}
精彩评论