PyQt 加载简单 Qt 设计器表单

2024-10-08 15:16:10 浏览数 (1)

在使用 PyQt 开发桌面应用程序时,使用 Qt Designer 可以大大加快 UI 的设计过程。你可以在 Qt Designer 中设计 UI,然后将其加载到 PyQt 中,从而实现分离的 UI 和逻辑代码。

问题背景

如何将一个简单的 Qt Designer 窗体加载到用 PySide 创建的 Qt 应用程序中。

解决方案

首先,使用 Qt Designer 创建一个简单的窗体,并将生成的代码从 .ui 文件复制到 .py 文件中。

代码语言:javascript复制
# -*- coding: utf-8 -*-
​
# Form implementation generated from reading ui file 'clientEditorForm.ui'
#
# Created: Tue Nov  3 23:46:41 2009
#      by: PySide uic UI code generator
#
# WARNING! All changes made in this file will be lost!
​
from PySide import QtCore, QtGui
​
class Ui_clientEditorForm(object):
    def setupUi(self, clientEditorForm):
        clientEditorForm.setObjectName("clientEditorForm")
        clientEditorForm.resize(575, 446)
        self.horizontalLayout_3 = QtGui.QHBoxLayout(clientEditorForm)
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        self.splitter = QtGui.QSplitter(clientEditorForm)
        self.splitter.setOrientation(QtCore.Qt.Horizontal)
        self.splitter.setObjectName("splitter")
        self.layoutWidget = QtGui.QWidget(self.splitter)
        self.layoutWidget.setObjectName("layoutWidget")
        self.verticalLayout = QtGui.QVBoxLayout(self.layoutWidget)
        self.verticalLayout.setObjectName("verticalLayout")
        self.horizontalLayout_2 = QtGui.QHBoxLayout()
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.searchLabel = QtGui.QLabel(self.layoutWidget)
        self.searchLabel.setObjectName("searchLabel")
        self.horizontalLayout_2.addWidget(self.searchLabel)
        self.searchEdit = QtGui.QLineEdit(self.layoutWidget)
        self.searchEdit.setObjectName("searchEdit")
        self.horizontalLayout_2.addWidget(self.searchEdit)
        self.clearButton = QtGui.QPushButton(self.layoutWidget)
        self.clearButton.setObjectName("clearButton")
        self.horizontalLayout_2.addWidget(self.clearButton)
        self.verticalLayout.addLayout(self.horizontalLayout_2)
        self.clientListView = QtGui.QListView(self.layoutWidget)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.clientListView.sizePolicy().hasHeightForWidth())
        self.clientListView.setSizePolicy(sizePolicy)
        self.clientListView.setObjectName("clientListView")
        self.verticalLayout.addWidget(self.clientListView)
        self.frame = QtGui.QFrame(self.splitter)
        self.frame.setFrameShape(QtGui.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtGui.QFrame.Raised)
        self.frame.setObjectName("frame")
        self.verticalLayout_3 = QtGui.QVBoxLayout(self.frame)
        self.verticalLayout_3.setObjectName("verticalLayout_3")
        self.formLayout = QtGui.QFormLayout()
        self.formLayout.setObjectName("formLayout")
        self.nameEdit = QtGui.QLineEdit(self.frame)
        self.nameEdit.setObjectName("nameEdit")
        self.formLayout.setWidget(0, QtGui.QFormLayout.FieldRole, self.nameEdit)
        self.nameLabel = QtGui.QLabel(self.frame)
        self.nameLabel.setObjectName("nameLabel")
        self.formLayout.setWidget(0, QtGui.QFormLayout.LabelRole, self.nameLabel)
        self.profileButton = QtGui.QLabel(self.frame)
        self.profileButton.setObjectName("profileButton")
        self.formLayout.setWidget(1, QtGui.QFormLayout.LabelRole, self.profileButton)
        self.addressLabel = QtGui.QLabel(self.frame)
        self.addressLabel.setObjectName("addressLabel")
        self.formLayout.setWidget(2, QtGui.QFormLayout.LabelRole, self.addressLabel)
        self.locationLabel = QtGui.QLabel(self.frame)
        self.locationLabel.setObjectName("locationLabel")
        self.formLayout.setWidget(3, QtGui.QFormLayout.LabelRole, self.locationLabel)
        self.secureLabel = QtGui.QLabel(self.frame)
        self.secureLabel.setObjectName("secureLabel")
        self.formLayout.setWidget(4, QtGui.QFormLayout.LabelRole, self.secureLabel)
        self.UUIDLabel = QtGui.QLabel(self.frame)
        self.UUIDLabel.setObjectName("UUIDLabel")
        self.formLayout.setWidget(5, QtGui.QFormLayout.LabelRole, self.UUIDLabel)
        self.passwordLabel = QtGui.QLabel(self.frame)
        self.passwordLabel.setObjectName("passwordLabel")
        self.formLayout.setWidget(6, QtGui.QFormLayout.LabelRole, self.passwordLabel)
        self.profileComboBox = QtGui.QComboBox(self.frame)
        self.profileComboBox.setObjectName("profileComboBox")
        self.formLayout.setWidget(1, QtGui.QFormLayout.FieldRole, self.profileComboBox)
        self.addressEdit = QtGui.QLineEdit(self.frame)
        self.addressEdit.setObjectName("addressEdit")
        self.formLayout.setWidget(2, QtGui.QFormLayout.FieldRole, self.addressEdit)
        self.locationComboBox = QtGui.QComboBox(self.frame)
        self.locationComboBox.setObjectName("locationComboBox")
        self.formLayout.setWidget(3, QtGui.QFormLayout.FieldRole, self.locationComboBox)
        self.secureComboBox = QtGui.QComboBox(self.frame)
        self.secureComboBox.setObjectName("secureComboBox")
        self.formLayout.setWidget(4, QtGui.QFormLayout.FieldRole, self.secureComboBox)
        self.UUIDEdit = QtGui.QLineEdit(self.frame)
        self.UUIDEdit.setObjectName("UUIDEdit")
        self.formLayout.setWidget(5, QtGui.QFormLayout.FieldRole, self.UUIDEdit)
        self.passwordEdit = QtGui.QLineEdit(self.frame)
        self.passwordEdit.setObjectName("passwordEdit")
        self.formLayout.setWidget(6, QtGui.QFormLayout.FieldRole, self.passwordEdit)
        self.verticalLayout_3.addLayout(self.formLayout)
        self.aliasesGroupBox = QtGui.QGroupBox(self.frame)
        self.aliasesGroupBox.setFlat(False)
        self.aliasesGroupBox.setCheckable(False)
        self.aliasesGroupBox.setObjectName("aliasesGroupBox")
        self.verticalLayout_2 = QtGui.QVBoxLayout(self.aliasesGroupBox)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.aliasesListView = QtGui.QListView(self.aliasesGroupBox)
        self.aliasesListView.setObjectName("aliasesListView")
        self.verticalLayout_2.addWidget(self.aliasesListView)
        self.horizontalLayout_4 = QtGui.QHBoxLayout()
        self.horizontalLayout_4.setObjectName("horizontalLayout_4")
        spacerItem = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.horizontalLayout_4.addItem(spacerItem)
        self.addButton = QtGui.QPushButton(self.aliasesGroupBox)
        self.addButton.setObjectName("addButton")
        self.horizontalLayout_4.addWidget(self.addButton)
        self.removeButton = QtGui.QPushButton(self.aliasesGroupBox)
        self.removeButton.setObjectName("removeButton")
        self.horizontalLayout_4.addWidget(self.removeButton)
        self.verticalLayout_2.addLayout(self.horizontalLayout_4)
        self.verticalLayout_3.addWidget(self.aliasesGroupBox)
        spacerItem1 = QtGui.QSpacerItem(20, 38, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
        self.verticalLayout_3.addItem(spacerItem1)
        self.horizontalLayout = QtGui.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        spacerItem2 = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.horizontalLayout.addItem(spacerItem2)
        self.revertButton = QtGui.QPushButton(self.frame)
        self.revertButton.setObjectName("revertButton")
        self.horizontalLayout.addWidget(self.revertButton)
        self.applyButton = QtGui.QPushButton(self.frame)
        self.applyButton.setObjectName("applyButton")
        self.horizontalLayout.addWidget(self.applyButton)
        self.verticalLayout_3.addLayout(self.horizontalLayout)
        self.horizontalLayout_3.addWidget(self.splitter)
        self.searchLabel.setBuddy(self.searchEdit)
        self.nameLabel

其实我要说的就是有两种主要方式可以在 PyQt 中加载 Qt Designer 表单:

1、.ui 文件转换为 Python 代码:使用 pyuic5 将 Qt Designer 生成的 .ui 文件转换为 Python 文件,然后在代码中直接使用。

2、动态加载 .ui 文件:通过 uic.loadUi() 函数动态加载 .ui 文件,不需要预先转换。

这两种方式都可以让我们在 PyQt 应用程序中轻松使用 Qt Designer 设计的界面,并将界面设计和业务逻辑分离。

0 人点赞