KeypressEvent 不起作用,如何纠正?

发布于 2024-11-14 17:45:39 字数 1521 浏览 2 评论 0原文

我正在尝试使用以下代码让 keypressevent 工作

import sys,
from PyQt4 import QtCore, QtGui

class Ui_MainWindow(QtGui.QWidget):
    def __init__(self):
        super(Ui_MainWindow, self).__init__()


    def keyPressEvent(self, event):
        print 'a'

    def setupUi(self, MainWindow):
        MainWindow.setObjectName(("MainWindow"))
        MainWindow.resize(371, 345)
        MainWindow.setMaximumSize(QtCore.QSize(401, 600))
        MainWindow.setWindowIcon(QtGui.QIcon('icons/icon.png'))
        screen = QtGui.QDesktopWidget().screenGeometry()
        mysize = MainWindow.geometry()
        hpos = ( screen.width() - mysize.width() ) / 2
        vpos = ( screen.height() - mysize.height() ) / 2
        MainWindow.move(hpos, vpos)
        #some GUI
        MainWindow.setCentralWidget(self.centralwidget)
        cd=MainWindow.centralWidget()
        cd.setFocusPolicy(QtCore.Qt.StrongFocus)
        cd.setFocus()
        self.actionHardware = QtGui.QAction(MainWindow)
        self.actionHardware.setObjectName(("actionHardware"))
        self.retranslateUi(MainWindow)


        #COnnect odes


    def retranslateUi(self, MainWindow):
        #sime button text codes

if __name__=="__main__" :
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

该代码是使用 QTDesigner 部分生成的。我注意到用 ui.show() 替换 Mainwindow.show() 可以启用 keypressevent,但代价是不显示我在 Mainwindow 中央小部件中创建的任何按钮

I am trying to get keypressevent work with the following code

import sys,
from PyQt4 import QtCore, QtGui

class Ui_MainWindow(QtGui.QWidget):
    def __init__(self):
        super(Ui_MainWindow, self).__init__()


    def keyPressEvent(self, event):
        print 'a'

    def setupUi(self, MainWindow):
        MainWindow.setObjectName(("MainWindow"))
        MainWindow.resize(371, 345)
        MainWindow.setMaximumSize(QtCore.QSize(401, 600))
        MainWindow.setWindowIcon(QtGui.QIcon('icons/icon.png'))
        screen = QtGui.QDesktopWidget().screenGeometry()
        mysize = MainWindow.geometry()
        hpos = ( screen.width() - mysize.width() ) / 2
        vpos = ( screen.height() - mysize.height() ) / 2
        MainWindow.move(hpos, vpos)
        #some GUI
        MainWindow.setCentralWidget(self.centralwidget)
        cd=MainWindow.centralWidget()
        cd.setFocusPolicy(QtCore.Qt.StrongFocus)
        cd.setFocus()
        self.actionHardware = QtGui.QAction(MainWindow)
        self.actionHardware.setObjectName(("actionHardware"))
        self.retranslateUi(MainWindow)


        #COnnect odes


    def retranslateUi(self, MainWindow):
        #sime button text codes

if __name__=="__main__" :
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

The code was partially generated using QTDesigner. I noticed that replacing Mainwindow.show() by ui.show() enables keypressevent but at the cost of not showing any buttons i create in the Mainwindow central widget

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱冒险 2024-11-21 17:45:39

看来问题出在您重新使用设计器输出的代码的方式上。您定义了 Ui_MainWindow.keyPressEvent,并创建了该类的一个实例“ui”。但是:“ui”永远不会在任何地方直接合并到 GUI 中(ui.setupUi 将其他小部件添加到 MainWindow,但不会将其本身添加到 MainWindow),因此事件永远不会传递到 ui。

我的方法看起来更像是这样的:

class Ui_MainWindow(object):  ## note this does not need to inherit QWidget
    ...                       ## and ideally, this code should not be changed
    ...                       ## after designer generates it
    ...

class Window(QtGui.QMainWindow):
    def __init__(self):
        super(Window, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

    def keyPressEvent(self, ev):
        print "key press"

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    MainWindow = Window()
    MainWindow.show()
    sys.exit(app.exec_())

It looks like the problem is in the way you are re-using the code output by Designer. You defined Ui_MainWindow.keyPressEvent, and created an instance "ui" of the class. However: "ui" is never directly incorporated into the GUI anywhere (ui.setupUi adds other widgets, but not itself, to MainWindow) and thus events are never delivered to ui.

My approach would look more like this:

class Ui_MainWindow(object):  ## note this does not need to inherit QWidget
    ...                       ## and ideally, this code should not be changed
    ...                       ## after designer generates it
    ...

class Window(QtGui.QMainWindow):
    def __init__(self):
        super(Window, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

    def keyPressEvent(self, ev):
        print "key press"

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    MainWindow = Window()
    MainWindow.show()
    sys.exit(app.exec_())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文