Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing key events

Tags:

c++

events

qt

I have a simple application with just one QPlainTextEdit, basically the same as Qt's example here:

http://qt-project.org/doc/qt-5.1/qtwidgets/mainwindows-application.html

When I press Ctrl+Z, it calls undo. When I press Ctrl+A, it selects all text. This is ok.

But when I press Ctrl+E or Ctrl+R, which are not defined in the menu, the characters "e" and "r" will appear in the QSimpleTextEdit.

How do I prevent this? How to "filter" keypresses which have been defined as menu shortcuts and keep them working, and "prevent" those keypresses not defined as menu shortcuts from appearing in the edit?

like image 216
Tuom L. Avatar asked Oct 10 '13 12:10

Tuom L.


People also ask

How can we prevent events?

Calling preventDefault() during any stage of event flow cancels the event, meaning that any default action normally taken by the implementation as a result of the event will not occur. You can use Event. cancelable to check if the event is cancelable. Calling preventDefault() for a non-cancelable event has no effect.

What is key event handling?

This low-level event is generated by a component object (such as a text field) when a key is pressed, released, or typed. The event is passed to every KeyListener or KeyAdapter object which registered to receive such events using the component's addKeyListener method.

How do I turn off keypress events?

How do I turn off keypress events? Alternatively, you could attach another event handler to the input field, and in this handler stop the propagation of the event: jQuery('#input-field-id'). bind('keypress', function(e) { e. stopPropagation(); });

Why do we use preventDefault?

The preventDefault() method cancels the event if it is cancelable, meaning that the default action that belongs to the event will not occur. For example, this can be useful when: Clicking on a "Submit" button, prevent it from submitting a form. Clicking on a link, prevent the link from following the URL.


1 Answers

There are 2 options:

1) Create a subclass and reimplement keyPressEvent()

2) Create an eventFilter and use installEventFilter() (see http://qt-project.org/doc/qt-5.0/qtcore/qobject.html#installEventFilter)

like image 95
Sebastian Lange Avatar answered Oct 19 '22 10:10

Sebastian Lange