在QWidg中实现keyPressEvent

2024-04-19 09:30:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个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上的所有文档。任何帮助都将不胜感激!


Tags: keyselfeventuidefwindowqt按钮
3条回答

对于阿卡扬的回答,我成功地使用了代码。 它可以是回车键和回车键。 尝试按回车键和回车键。它们在我的键盘上是不同的。

QtCore.QObject.connect(self.ui.search_box, QtCore.SIGNAL("textChanged()"), self.fucn1)

每当在search_box函数中更改文本时,就会调用fucn1。 为pyqt4工作

你可以做两种方法,一种是简单地重新实现keyPressevent而不需要任何花哨的工作。像这样

from PyQt4 import QtCore, QtGui
import sys

class Example(QtGui.QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.setGeometry(300, 300, 250, 150)
        self.show()

    def keyPressEvent(self, event):
        if event.key() == QtCore.Qt.Key_Q:
            print "Killing"
            self.deleteLater()
        elif event.key() == QtCore.Qt.Key_Enter:
            self.proceed()
        event.accept()

    def proceed(self):
        print "Call Enter Key"

def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

或者,当您尝试使用信号时,在您的情况下,如果您没有正确实现此信号,这里是更新版本。

class Example(QtGui.QWidget):
    keyPressed = QtCore.pyqtSignal(QtCore.QEvent)
    def __init__(self):
        super(Example, self).__init__()
        self.setGeometry(300, 300, 250, 150)
        self.show()
        self.keyPressed.connect(self.on_key)

    def keyPressEvent(self, event):
        super(Example, 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:
            print "Killing"
            self.deleteLater()  # a test I implemented to see if pressing 'Q' would close the window

    def proceed(self):
        print "Call Enter Key"

def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

相关问题 更多 >