主要问题
本实例主要解决的问题:Qt Designer设计完UI文件,逻辑代码继承编译后的UI类无法实现窗体控件随主窗体大小变化而缩放。
测试目标
1. 用Eric创建epj文件:test.epj
- UI文件,test.ui
- 编译后的UI文件,Ui_test.py
- 生成装饰器槽函数,test.py
- 编译resource.qrc
2. Qt Designer设计UI窗体和资源文件
- test.ui
- resource.qrc
- img:jpg,png,ico
- qss
3. 用Pycharm管理Python项目文件并运行测试;
文档结构
- test.ui, Qt Designer完成设计
- Ui_test.py, Eric完成编译
- test.py, Eric完成生成装饰器槽函数文件
- resource.qrc, Qt Designer完成设计
- style.qss, 可以用QSS Editor
代码部分
1. Qt Designer设计test.ui和resource.qrc
2. Eric完成编译后生成Ui_test.py, 代码文件代码如下
# Form implementation generated from reading ui file 'D:\Python Project\Eric Projects\test.ui'
#
# Created by: PyQt6 UI code generator 6.2.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt6 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 632)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("./img/test.png"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
MainWindow.setWindowIcon(icon)
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralWidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(0, 0, 801, 601))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.txtB = QtWidgets.QTextBrowser(self.verticalLayoutWidget)
self.txtB.setStyleSheet("")
self.txtB.setObjectName("txtB")
self.verticalLayout.addWidget(self.txtB)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.toolButton_4 = QtWidgets.QToolButton(self.verticalLayoutWidget)
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap("./img/a1.jpg"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
self.toolButton_4.setIcon(icon1)
self.toolButton_4.setIconSize(QtCore.QSize(120, 120))
self.toolButton_4.setObjectName("toolButton_4")
self.horizontalLayout_2.addWidget(self.toolButton_4)
self.toolButton_3 = QtWidgets.QToolButton(self.verticalLayoutWidget)
icon2 = QtGui.QIcon()
icon2.addPixmap(QtGui.QPixmap("./img/a2.jpg"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
self.toolButton_3.setIcon(icon2)
self.toolButton_3.setIconSize(QtCore.QSize(120, 120))
self.toolButton_3.setObjectName("toolButton_3")
self.horizontalLayout_2.addWidget(self.toolButton_3)
self.toolButton_2 = QtWidgets.QToolButton(self.verticalLayoutWidget)
icon3 = QtGui.QIcon()
icon3.addPixmap(QtGui.QPixmap("./img/a3.jpg"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
self.toolButton_2.setIcon(icon3)
self.toolButton_2.setIconSize(QtCore.QSize(120, 120))
self.toolButton_2.setObjectName("toolButton_2")
self.horizontalLayout_2.addWidget(self.toolButton_2)
self.toolButton = QtWidgets.QToolButton(self.verticalLayoutWidget)
icon4 = QtGui.QIcon()
icon4.addPixmap(QtGui.QPixmap("./img/a4.jpg"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
self.toolButton.setIcon(icon4)
self.toolButton.setIconSize(QtCore.QSize(120, 120))
self.toolButton.setObjectName("toolButton")
self.horizontalLayout_2.addWidget(self.toolButton)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.btn1 = QtWidgets.QPushButton(self.verticalLayoutWidget)
icon5 = QtGui.QIcon()
icon5.addPixmap(QtGui.QPixmap("./img/start.ico"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
self.btn1.setIcon(icon5)
self.btn1.setIconSize(QtCore.QSize(32, 32))
self.btn1.setObjectName("btn1")
self.horizontalLayout.addWidget(self.btn1)
spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Minimum)
self.horizontalLayout.addItem(spacerItem1)
self.btn2 = QtWidgets.QPushButton(self.verticalLayoutWidget)
icon6 = QtGui.QIcon()
icon6.addPixmap(QtGui.QPixmap("./img/stop.ico"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
self.btn2.setIcon(icon6)
self.btn2.setIconSize(QtCore.QSize(32, 32))
self.btn2.setObjectName("btn2")
self.horizontalLayout.addWidget(self.btn2)
spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Minimum)
self.horizontalLayout.addItem(spacerItem2)
self.verticalLayout.addLayout(self.horizontalLayout)
MainWindow.setCentralWidget(self.centralWidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "testWin"))
self.toolButton_4.setText(_translate("MainWindow", "PIC1"))
self.toolButton_3.setText(_translate("MainWindow", "PIC2"))
self.toolButton_2.setText(_translate("MainWindow", "PIC3"))
self.toolButton.setText(_translate("MainWindow", "PIC4"))
self.btn1.setText(_translate("MainWindow", "开始"))
self.btn2.setText(_translate("MainWindow", "停止"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec())
3. Eric生成独立的逻辑文件test.py(生成第一次可以自定义,之后不要再重新生成同样名字否则会覆盖自定义代码),代码如下:
# -*- coding: utf-8 -*-
"""
Module implementing MainWindow.
"""
from PyQt6.QtCore import pyqtSlot
from PyQt6.QtWidgets import QMainWindow, QApplication
from Ui_test import Ui_MainWindow
class MainWindow(QMainWindow, Ui_MainWindow):
"""
Class documentation goes here.
"""
def __init__(self, parent=None):
"""
Constructor
@param parent reference to the parent widget (defaults to None)
@type QWidget (optional)
"""
super().__init__(parent)
self.setupUi(self)
@pyqtSlot()
def on_btn1_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
print('you clicked btn1')
self.txtB.setText('you clicked btn1.')
@pyqtSlot()
def on_btn2_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
print('you clicked btn2')
self.txtB.setText('you clicked btn2.')
@pyqtSlot()
def on_toolButton_4_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
print('you clicked tbtn4')
self.txtB.setText('you clicked tbtn4.')
@pyqtSlot()
def on_toolButton_3_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
print('you clicked tbtn3')
self.txtB.setText('you clicked tbtn3.')
@pyqtSlot()
def on_toolButton_2_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
print('you clicked tbtn2')
self.txtB.setText('you clicked tbtn2.')
@pyqtSlot()
def on_toolButton_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
print('you clicked tbtn1')
self.txtB.setText('you clicked tbtn1.')
class CommonHelper: # 手动编写
"""
using for common module
用于加载qss文件
"""
def __init__(self):
pass
@staticmethod
def readQss(style):
"""
read style.qss file
"""
with open(style, 'r') as f:
return f.read()
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
styleFile = './qss/test.qss'
qssStyle = CommonHelper.readQss(styleFile)
ui = MainWindow()
ui.centralWidget.setLayout(ui.verticalLayout) # 将主窗体设置为垂直布局,随窗体变化
ui.setStyleSheet(qssStyle)
ui.show()
sys.exit(app.exec())
"""
以下代码直接实例化Ui_test.py模块中的Ui_MainWindow,用于测试布局随窗体大小变化自动变化
mainWindow = QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(mainWindow)
# 核心代码部分
ui.centralWidget.setLayout(ui.verticalLayout) # 将主窗体设置为垂直布局,随窗体变化
mainWindow.setCentralWidget(ui.centralWidget)
mainWindow.setStyleSheet(qssStyle)
mainWindow.show()
sys.exit(app.exec())
"""
核心代码,添加后可以实现控件随主窗体大小变化完成自动缩放。
ui.centralWidget.setLayout(ui.verticalLayout) # 将主窗体设置为垂直布局,随窗体变化
![](https://i-blog.csdnimg.cn/blog_migrate/80619963f5308b67b8f0379c63341b1b.png)
![](https://i-blog.csdnimg.cn/blog_migrate/db1a5b319fa0b59e372ed8990f53493a.png)