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. How to disable shortcuts in QTextEdit
Forum Updated to NodeBB v4.3 + New Features

How to disable shortcuts in QTextEdit

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 5.1k 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.
  • M Offline
    M Offline
    Muhammad
    wrote on last edited by
    #1

    Hi
    I'm working in a scientific calculator project using Qt5, I'm using the QTextEdit as the calculator's display.
    I want to disable the shortcuts like (Ctrl + A, and Ctrl + C) in the display, so How can I do that?
    Thank you.

    I don't have endpoint, I just have Checkpoints.

    1 Reply Last reply
    0
    • L Offline
      L Offline
      leon.anavi
      wrote on last edited by
      #2

      Check this similar thread for hints about handling key events: "how to catch actions with default shortcuts":http://qt-project.org/forums/viewthread/5089

      http://anavi.org/

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Muhammad
        wrote on last edited by
        #3

        Well I did Checked that thread, but I didn't understand what to do, so if there is any link to explain it in a more details it would be nice.
        Thanks

        I don't have endpoint, I just have Checkpoints.

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Well, there's really not much more to say about it since the code is quite exhaustive, but I'll try.

          There's no method like QTextEdit::disableThisOrThatKeyboardSequence() so we need to work around it a little. Qt has this notion of events - when something happens to object (like key press, mouse move etc.) it receives an event notification in a handler like mouseEvent(). Qt also offers a way to filter some of these events out and block them before they reach the addressed object.

          This filtering is done by installing an event filter(another qobject) on your object. That event filter object will receive all the events before they will reach the addressed object, and can block that from happening. We can use that to detect key sequences like ctrl+a and stop them from reaching the text edit.

          We install an event filter by calling yourQtextEditObject->installEventFilter(theFilteringObject). An event filter can be any QObject like your window, some other widget or a QObject descendant you create explicitely for that purpose. Then, in the theFilteringObject class we declare a member bool eventFilter(QObject *obj, QEvent *event). This is the member that will be called whenever an event is dispached to yourQtextEditObject.
          Inside of it we check if the obj is the qtextedit we are interested in (since one event filter object can serve multiple event recipients). If it is, we check for the event type (since we get all of them from mouse clicks, keyboard events to resizes etc.). If it is key event, we cast it to QKeyEvent and we check if it is a specific key sequence (like ctrl+a) with if(keyEvent == ...). If it is we return true (this blocks the event from reaching recipient). If it's not, we return false to let the event reach its destination and be handled by it.

          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