在QWidget中实现keyPressEvent [英] Implementing keyPressEvent in QWidget

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

问题描述

我有一个带有继续按钮的QDialog窗口.继续按钮是默认按钮,因为每当我按下Enter键时,都会按下继续按钮.我发现了一些奇怪的事情:当我按Enter键三下时,继续按钮也被按了三下.但是,当我第四次按下它时,整个窗口关闭.我在关闭窗口的继续按钮下面有一个取消按钮,但是我没有将取消按钮设置为默认按钮或其他任何按钮.

I have a QDialog window that has a continue button. The continue button is the default button because whenever I press the enter key, the continue button is pressed. I discovered something strange: when I press the enter key three times, the continue button presses three times. However, when I press it a fourth time, the whole window closes. I have a cancel button right below the continue button that closes the window, but I don't make the cancel button the default button or anything.

我想覆盖keyPressEvent,以便每当我在窗口中时,输入按钮将始终连接到继续按钮.

I wanted to override the keyPressEvent so that whenever I'm in the window, the enter button will always be connected to the continue button.

这就是我现在拥有的:

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"不会关闭窗口,而且我无法真正确定"enter"键是否起作用.

However, this doesn't seem to be doing anything right now. Pressing 'Q' doesn't close the window, and I can't really tell if the 'enter' key is working or not.

我事先看过这个问题: PyQt连接到KeyPressEvent

I looked at this question beforehand: PyQt Connect to KeyPressEvent

我还查看了SourceForge上的所有文档.任何帮助将不胜感激!

I also reviewed all the documentation on SourceForge. Any help would be greatly appreciated!

推荐答案

您可以执行两种方法,一种是简单地重新实现keyPressevent而无需做任何花哨的工作.像这样

You can do two ways and one is simply re implement keyPressevent with out any fancy work. Like this

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()

或者您尝试使用信号时,如果是您无法正确实现此信号的情况,则为更新版本.

Or as you tried with signals, in your case you where missing to implement this signal properly, here is updated version.

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()

这篇关于在QWidget中实现keyPressEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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