Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] QPlainTextEdit: Funny problem in intercepting Ctrl-F
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Harry123
    wrote on last edited by Harry123
    #1

    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 ?

    raven-worxR 1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

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

      H 1 Reply Last reply
      0
      • H Harry123

        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 ?

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by raven-worx
        #3

        @Harry123
        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 */ )
                            {
                                 e->accept();
                                 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.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        H 1 Reply Last reply
        0
        • ? A Former User

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

          H Offline
          H Offline
          Harry123
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • raven-worxR raven-worx

            @Harry123
            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 */ )
                                {
                                     e->accept();
                                     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.

            H Offline
            H Offline
            Harry123
            wrote on last edited by
            #5

            @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.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved