在子类 PyQT LineEdit 中正确处理 keyPressEvent
所以我有一个 QLineEdit,我想在其中捕获 Shift 按键。
这是我的代码:
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 中输入文本。我尝试过捕获按键,但我不确定如何处理它们以允许用户继续在小部件中输入内容。
我缺少什么?谢谢!
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 to continue to type into the widget.
What am I missing? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜你想要重写的 keyPressEvent 方法的默认行为,你应该调用基类实现,像这样:
希望这有帮助,问候
I guess you want the default behavior of the overridden keyPressEvent method you should call the base class implementation, smth like this:
hope this helps, regards