【发布时间】:2016-11-25 05:02:44
【问题描述】:
我有一个带有继续按钮的 QDialog 窗口。继续按钮是默认按钮,因为每当我按下回车键时,都会按下继续按钮。我发现了一些奇怪的东西:当我按三下回车键时,继续按钮按了三下。但是,当我第四次按下它时,整个窗口都会关闭。我在关闭窗口的继续按钮下方有一个取消按钮,但我没有将取消按钮设为默认按钮或任何其他按钮。
我想覆盖keyPressEvent,这样每当我在窗口中时,输入按钮将始终连接到继续按钮。
这就是我现在拥有的:
class ManualBalanceUI(QtGui.QWidget):
keyPressed = QtCore.pyqtSignal()
def __init__(self, cls):
super(QtGui.QWidget, self).__init__()
self.window = QtGui.QDialog(None, QtCore.Qt.WindowSystemMenuHint)
self.ui = uic.loadUi('ManualBalanceUI.ui', self.window)
self.keyPressed.connect(self.on_key)
def keyPressEvent(self, event):
super(ManualBalanceUI, self).keyPressEvent(event)
self.keyPressed.emit(event)
def on_key(self, event):
if event.key() == QtCore.Qt.Key_Enter and self.ui.continueButton.isEnabled():
self.proceed() # this is called whenever the continue button is pressed
elif event.key() == QtCore.Qt.Key_Q:
self.window.close() # a test I implemented to see if pressing 'Q' would close the window
def proceed(self):
...
...
但是,现在这似乎没有任何作用。按“Q”不会关闭窗口,我也无法确定“回车”键是否有效。
我之前看过这个问题:PyQt Connect to KeyPressEvent
我还查看了 SourceForge 上的所有文档。任何帮助将不胜感激!
【问题讨论】:
标签: python event-handling pyqt keypress