我从PyQt GPL v5.4创建一个UI,并使用pyuic5 convert * .ui文件到* .py
但我不知道keyPressEvent在这段代码中是如何工作的!
它应该适用于QWidget,但是如何让它工作。
请帮忙!
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QWidget
class Ui_MainWindow(QWidget,object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(200, 200)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(50, 110, 75, 23))
self.pushButton.setObjectName("pushButton")
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))\
def keyPressEvent(self, e):
if e.key() == Qt.Key_F5:
self.close()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QWidget()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
答案 0 :(得分:3)
在开始我的答案之前的建议,不要修改生成Qt Designer的类,在你的情况下,我认为你使用模板MainWindow的名称,在下面的代码中我添加了一些你删除的代码,什么你必须做的是创建一个实现生成视图的新类:
查看:强>
!ENTRY org.grails.ide.eclipse.core 4 0 2017-07-25 18:43:19.426
!MESSAGE Refresh dependecies failed
!STACK 0
java.lang.NoClassDefFoundError: org/codehaus/groovy/frameworkadapter/util/SpecifiedVersion
at org.grails.ide.eclipse.commands.GroovyCompilerVersionCheck.getRequiredGroovyVersion(GroovyCompilerVersionCheck.java:63)
at org.grails.ide.eclipse.commands.GroovyCompilerVersionCheck.getRequiredGroovyVersion(GroovyCompilerVersionCheck.java:57)
at org.grails.ide.eclipse.commands.GroovyCompilerVersionCheck.check(GroovyCompilerVersionCheck.java:50)
at org.grails.ide.eclipse.commands.GrailsCommandUtils.refreshDependencies(GrailsCommandUtils.java:327)
at org.grails.ide.eclipse.core.internal.classpath.GrailsClasspathContainerUpdateJob.runInWorkspace(GrailsClasspathContainerUpdateJob.java:89)
at org.eclipse.core.internal.resources.InternalWorkspaceJob.run(InternalWorkspaceJob.java:38)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)
在QMainWindow的情况下,实现视图的类必须从模板的类继承,并且除了调用父构造函数之外,还使用setupUI方法,即在QMainWindow的情况下。
<强>逻辑:强>
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(200, 200)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.setCentralWidget(self.centralwidget)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(50, 110, 75, 23))
self.pushButton.setObjectName("pushButton")
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
通过这些修改,keyPressEvent方法已经有效。