Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    How to disable shortcuts in QTextEdit

    General and Desktop
    3
    4
    4765
    Loading More Posts
    • 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
      Muhammad last edited by

      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 Reply Quote 0
      • L
        leon.anavi last edited by

        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 Reply Quote 0
        • M
          Muhammad last edited by

          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 Reply Quote 0
          • Chris Kawa
            Chris Kawa Moderators last edited by

            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 Reply Quote 0
            • First post
              Last post