简体   繁体   中英

Preventing Enter key from triggering OK in QButtonBox in particular QLineEdit QButtonBox

I have a Dialog which has some widgets such as QComboBox , QSpinBox 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;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM