正确处理子类 PyQT LineEdit 中的 keyPressEvent [英] Properly handling a keyPressEvent in a Subclassed PyQT LineEdit

查看:55
本文介绍了正确处理子类 PyQT LineEdit 中的 keyPressEvent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个 QLineEdit,我想捕捉一个 shift 按键.

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

这是我的代码:

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!'

如您所料,我可以捕捉到 shift 按键,但是您无法在 LineEdit 中输入文本.我试过捕捉按键,但我不太确定如何处理它们以允许用户继续在小部件中输入.

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 to continue to type into the widget.

我错过了什么?谢谢!

推荐答案

我猜你想要重写的 keyPressEvent 方法的默认行为,你应该调用基类实现,像这样:

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

这篇关于正确处理子类 PyQT LineEdit 中的 keyPressEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆