开发者

Properly handling a keyPressEvent in a Subclassed PyQT LineEdit

开发者 https://www.devze.com 2023-02-12 13:34 出处:网络
So I have a QLineEdit that I want to catch a shift keypress in. Here\'s my code: class NoteText(QtGui.QLineEdit):

So I have a QLineEdit that I want to catch a shift keypress in.

Here's 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 keypress, but then you cannot input text into the LineEdit. I've tried catching keypresses, but I'm not sure quite what to do with them to allow the user开发者_JS百科 to continue to type into the widget.

What am I missing? Thanks!


I guess you want the default behavior of the overridden keyPressEvent method you should 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, regards

0

精彩评论

暂无评论...
验证码 换一张
取 消