Correct handling of the PressEvent key in a subclass of PyQT LineEdit

So, I have a QLineEdit that I want to catch by pressing the shift key.

Here is my code:

class NoteText(QtGui.QLineEdit): def __init__(self, parent): super (NoteText, self).__init__(parent) def keyPressEvent(self, event): if (event.modifiers() & QtCore.Qt.ShiftModifier): self.shift = True print 'Shift!' 

As you can guess, I can catch the shift shift, but then you cannot enter text in LineEdit. I tried to catch keystrokes, but I'm not sure what to do with them to allow the user to continue typing into the widget.

What am I missing? Thanks!

+4
source share
1 answer

I think you want the default behavior of the overridden keyPressEvent method to call the base class implementation, smth like this:

 def keyPressEvent(self, event): if (event.modifiers() & QtCore.Qt.ShiftModifier): self.shift = True print 'Shift!' # call base class keyPressEvent QtGui.QLineEdit.keyPressEvent(self, event) 

hope this helps, believes

+6
source

Source: https://habr.com/ru/post/1340398/


All Articles