从 QtCreator 覆盖 PyQt 中的 QMainWindow 方法
Overriding QMainWindow methods in PyQt from QtCreator
我得到了由 pyuic5
使用 -x
参数生成的 GUI 代码。我已将我的一些代码添加到 Ui_LEDController
,例如通过串口连接到 arduino。我想通过 X
.
在程序关闭时实现断开 Arduino
我知道我必须覆盖 QMainWindow
方法 closeEvent()
,但我想知道我应该采取哪些步骤来实现这一目标。我不能只创建 class MyWindow(QtWidgets.QMainWindow())
然后 LEDController = MyWindow()
,因为我将无法访问串行变量。
所以基本上:我不应该使用这个 Ui_LEDController
来设置 GUI 项,而不是创建 class MyWindow(QtWidgets.QMainWindow())
,我将在其中实现所有 GUI 项并覆盖 closeEvent()
?
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_LEDController(object):
def setupUi(self, LEDController):
LEDController.setObjectName("LEDController")
LEDController.resize(230, 160)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(LEDController.sizePolicy().hasHeightForWidth())
LEDController.setSizePolicy(sizePolicy)
LEDController.setMinimumSize(QtCore.QSize(230, 160))
LEDController.setMaximumSize(QtCore.QSize(230, 160))
# Lot of initializing
LEDController.setCentralWidget(self.central_widget)
self.retranslateUi(LEDController)
# Signals and Slots handlers
QtCore.QMetaObject.connectSlotsByName(LEDController)
def retranslateUi(self, LEDController):
_translate = QtCore.QCoreApplication.translate
LEDController.setWindowTitle(_translate("LEDController", "LEDController"))
self.bright_label.setText(_translate("LEDController", "Brightness"))
# Changing text in items
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
LEDController = QtWidgets.QMainWindow()
ui = Ui_LEDController()
ui.setupUi(LEDController)
LEDController.show()
sys.exit(app.exec_())
你至少有两种方法:
QMainWindow
和 Ui_LEDController
的子类
class MyWindowClass(QMainWindow, Ui_LEDController):
或仅具有属性 self.ui
的子类 QMainWindow
class MyWindowClass(QMainWindow):
def __init__(self):
...
self.ui = Ui_LEDController()
self.ui.setupUi(self)
然后使用类
的所有变量
我得到了由 pyuic5
使用 -x
参数生成的 GUI 代码。我已将我的一些代码添加到 Ui_LEDController
,例如通过串口连接到 arduino。我想通过 X
.
在程序关闭时实现断开 Arduino
我知道我必须覆盖 QMainWindow
方法 closeEvent()
,但我想知道我应该采取哪些步骤来实现这一目标。我不能只创建 class MyWindow(QtWidgets.QMainWindow())
然后 LEDController = MyWindow()
,因为我将无法访问串行变量。
所以基本上:我不应该使用这个 Ui_LEDController
来设置 GUI 项,而不是创建 class MyWindow(QtWidgets.QMainWindow())
,我将在其中实现所有 GUI 项并覆盖 closeEvent()
?
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_LEDController(object):
def setupUi(self, LEDController):
LEDController.setObjectName("LEDController")
LEDController.resize(230, 160)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(LEDController.sizePolicy().hasHeightForWidth())
LEDController.setSizePolicy(sizePolicy)
LEDController.setMinimumSize(QtCore.QSize(230, 160))
LEDController.setMaximumSize(QtCore.QSize(230, 160))
# Lot of initializing
LEDController.setCentralWidget(self.central_widget)
self.retranslateUi(LEDController)
# Signals and Slots handlers
QtCore.QMetaObject.connectSlotsByName(LEDController)
def retranslateUi(self, LEDController):
_translate = QtCore.QCoreApplication.translate
LEDController.setWindowTitle(_translate("LEDController", "LEDController"))
self.bright_label.setText(_translate("LEDController", "Brightness"))
# Changing text in items
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
LEDController = QtWidgets.QMainWindow()
ui = Ui_LEDController()
ui.setupUi(LEDController)
LEDController.show()
sys.exit(app.exec_())
你至少有两种方法:
QMainWindow
和 Ui_LEDController
class MyWindowClass(QMainWindow, Ui_LEDController):
或仅具有属性 self.ui
QMainWindow
class MyWindowClass(QMainWindow):
def __init__(self):
...
self.ui = Ui_LEDController()
self.ui.setupUi(self)
然后使用类
的所有变量