[Solved] QPlainTextEdit: Funny problem in intercepting Ctrl-F

  • I might be doing something wrong, but I cannot manage to intercept Ctrl-F from a sub-classed QPlainTextEdit.

    What I have tried :

    • Check for it in both keyPressEvent() and event(), where my code looks like
      if (pevent->key() == Qt::Key_F && (pevent->modifiers() & Qt::ControlModifier))
    • Added it as a menu item with Ctrl-F as the shortcut : the menu item works fine but not Ctrl-F
    • Added a check in the same way for Ctrl-A : this works perfectly
    • Check (just in case it was renamed) for the keys Qt::Key_Search and Qt::Key_Find : no go

    Is my check wrong in any way?
    Does QPlainTextEdit swallow this key combination ? Or QMainWindow in which it is embedded?
    Failing all else, is there any other way to detect Ctrl-F ?

  • Maybe Ctrl+F is a global shortcut of your desktop environment?

  • Moderators

    since you are also using it as a shortcur in the menu you might probably also check for a QEvent::ShortcutOverride event.

    bool MyPlainTextEdit::event(QEvent* e)
        switch( e->type()  )
               case QEvent::KeyPress:
               case QEvent::ShortcutOverride:
                         QKeyEvent* ke = static_cast<QKeyEvent*>( e );
                         // ...
                        if( /* ke == Ctrl+F */ )
                             return true;
         return QPlainTextEdit::event( e );

    The menu item's shortcurt is supposed to do the a different action than the in the text edit right?
    If so you can simply set the same QShortcut (taken from the menu item's QAction) and set it on the widget.

  • @Wieland Ctrl-F works in other programs than Qt.

  • @raven-worx
    This is not the case, but your comment started me digging everywhere in this rather large program, and I found the same shortcut defined elsewhere. Many thanks.

    Problem solved - programmer error.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.