How to disable shortcuts in QTextEdit
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?
Check this similar thread for hints about handling key events: "how to catch actions with default shortcuts":http://qt-project.org/forums/viewthread/5089
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.
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.