1.动态加载 ui 方式,重写关闭窗体事件

1. 静态加载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from PySide6.QtCore import *
from PySide6.QtGui import *
from PySide6.QtWidgets import *


class Ui_Form(object):
def setupUi(self, Form):
if not Form.objectName():
Form.setObjectName(u"Form")
Form.resize(732, 584)
self.verticalLayout = QVBoxLayout(Form)
self.verticalLayout.setObjectName(u"verticalLayout")
self.textEdit = QPlainTextEdit(Form)
self.textEdit.setObjectName(u"textEdit")

self.verticalLayout.addWidget(self.textEdit)

self.button = QPushButton(Form)
self.button.setObjectName(u"button")

self.verticalLayout.addWidget(self.button)
self.retranslateUi(Form)
QMetaObject.connectSlotsByName(Form)
# setupUi

def retranslateUi(self, Form):
Form.setWindowTitle(QCoreApplication.translate("Form", u"\u7edf\u8ba1\u85aa\u8d44", None))
self.textEdit.setPlaceholderText(QCoreApplication.translate("Form", u"\u8bf7\u8f93\u5165\u85aa\u8d44\u4fe1\u606f", None))
self.button.setText(QCoreApplication.translate("Form", u"\u7edf\u8ba1", None))
# retranslateUi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 静态加载py窗口函数  同时继承重写
from PySide6.QtWidgets import QApplication, QMessageBox, QWidget
from PySide6.QtUiTools import QUiLoader
from PySide6.QtCore import Qt
import sys
from stats import Ui_Form


class Win(Ui_Form, QWidget):
def __init__(self):
super().__init__()
# 第一种
self.setupUi(self)
# 第二种 可以把该界面的按钮功能和其他界面区分
# self.ui = Ui_Form()
# self.ui.setupUi(self)

def closeEvent(self, event):
reply = QMessageBox.question(self, '关闭提示', "是否要退出界面?",
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)

if reply == QMessageBox.Yes:
event.accept()
elif reply == QMessageBox.No:
event.ignore()


if __name__ == '__main__':
app = QApplication(sys.argv)
window = Win()
window.setWindowFlags(Qt.Window)
window.show()
sys.exit(app.exec_())

2. 动态加载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 动态加载py窗口函数  同时继承重写
from PySide6.QtWidgets import QApplication, QMessageBox, QWidget
from PySide6.QtUiTools import loadUiType
from PySide6.QtCore import Qt
import sys

InitUi, _ = loadUiType("stats.ui")


class Win(InitUi, QWidget):
def __init__(self):
super().__init__()
self.setupUi(self)

def closeEvent(self, event):
reply = QMessageBox.question(self, '关闭提示', "是否要退出界面?",
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
event.accept()
elif reply == QMessageBox.No:
event.ignore()


if __name__ == '__main__':
app = QApplication(sys.argv)
window = Win()
window.setWindowFlags(Qt.Window)
window.show()
sys.exit(app.exec_())
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from PySide2.QtWidgets import QApplication, QMessageBox
from PySide2.QtUiTools import QUiLoader
import sys


class Win:

def __init__(self):
self.ui = QUiLoader().load("stats.ui")


if __name__ == '__main__':
app = QApplication(sys.argv)
window = Win()
window.setWindowFlags(Qt.Window)
window.show()
sys.exit(app.exec_())

按照此方法不成功是因为我们在载入ui文件时,使用了 QuiLoader().load()方法,这个方法不能够接收主窗体(MainWindow)的 Close() 事件,所以需要重写 QuiLoader() 方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 代码段2
class UiLoader(QtUiTools.QUiLoader):
# ui实例
uiinstance = None

# 重写方法
def createWidget(self, classname, parent=None, name=''):
# 判断是否存在
if parent is None and self.uiinstance is not None:
widget = self.uiinstance
else:
widget = super(UiLoader, self).createWidget(classname, parent, name)
if self.uiinstance is not None:
setattr(self.uiinstance, name, widget)
return widget

# 加载UI
def loadUi(self, uifile, ui=None):
self.uiinstance = ui
widget = self.load(uifile)
QtCore.QMetaObject.connectSlotsByName(widget)
return widget
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from PySide2.QtWidgets import QApplication, QMessageBox
from PySide2.QtUiTools import QUiLoader
import sys


class Win:

def __init__(self):
qfile_bom = QFile("ui/BomSoftware.ui") # 指定路径
qfile_bom.open(QFile.ReadWrite)
qfile_bom.close()
# self.ui = QUiLoader().load(qfile_bom) 原先的加载ui方式
self.ui = UiLoader().loadUi(qfile_bom, self) # 使用重写的loadUI方法


if __name__ == '__main__':
app = QApplication(sys.argv)
window = Win()
window.setWindowFlags(Qt.Window)
window.show()
sys.exit(app.exec_())