我有一个QDialog窗口,它有一个continue按钮。“继续”按钮是默认按钮,因为每当我按回车键时,都会按“继续”按钮。我发现了一件奇怪的事情:当我按三次回车键时,continue按钮按了三次。然而,当我第四次按下它时,整个窗口都关闭了。我在关闭窗口的continue按钮下面有一个cancel按钮,但是我没有将cancel按钮设为默认按钮或其他任何按钮。
我想重写keyPressEvent
,这样每当我在窗口中时,enter按钮将始终连接到continue按钮。
这就是我现在所拥有的:
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上的所有文档。任何帮助都将不胜感激!
对于阿卡扬的回答,我成功地使用了代码。 它可以是回车键和回车键。 尝试按回车键和回车键。它们在我的键盘上是不同的。
每当在
search_box
函数中更改文本时,就会调用fucn1
。 为pyqt4工作你可以做两种方法,一种是简单地重新实现keyPressevent而不需要任何花哨的工作。像这样
或者,当您尝试使用信号时,在您的情况下,如果您没有正确实现此信号,这里是更新版本。
相关问题 更多 >
编程相关推荐