从预定义布局将选项卡插入 QTabWidget

Inserting a tab into QTabWidget from a pre-defined layout

我正在尝试动态地向我的 QTabWidget 添加选项卡,我想做的简单事情就是复制我已经拥有的第一个预设选项卡。在你引导我回答这个问题之前:,请听我说完。

我不想手动复制标签。我拥有的选项卡很详细,可以随时对其进行重大更新,我想简单地在 QTDesigner 中编辑它,然后动态复制它或简单地将给定的表单插入 QTabWidget。我已经尝试了很多事情(即:尝试制作一个单独的表单并导入它,设计我自己的标签页 class [有效但不是我想要的],从字面上复制 tabWidget 并尝试添加它)但其中 none 似乎可以解决问题。

我只需要通过预定义的 UI.

插入一个标签

编辑:

通过 QtDesigner 软件创建表单并将其导出到 python 文件后,我得到以下代码:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'newTab.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(810, 672)
        self.verticalLayout = QtWidgets.QVBoxLayout(Form)
        self.verticalLayout.setObjectName("verticalLayout")
        self.testIdFrame = QtWidgets.QFrame(Form)
        self.testIdFrame.setMinimumSize(QtCore.QSize(0, 622))
        font = QtGui.QFont()
        font.setFamily("Segoe UI Black")
        font.setBold(True)
        font.setItalic(True)
        font.setUnderline(False)
        font.setWeight(75)
        self.testIdFrame.setFont(font)
        self.testIdFrame.setStyleSheet("background: transparent;")
        self.testIdFrame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.testIdFrame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.testIdFrame.setObjectName("testIdFrame")
        self.atpDropBox = QtWidgets.QComboBox(self.testIdFrame)
        self.atpDropBox.setGeometry(QtCore.QRect(120, 20, 98, 31))
        self.atpDropBox.setObjectName("atpDropBox")
        self.snLabel = QtWidgets.QLabel(self.testIdFrame)
        self.snLabel.setGeometry(QtCore.QRect(260, 30, 32, 21))
        self.snLabel.setObjectName("snLabel")
        self.workOrderLabel = QtWidgets.QLabel(self.testIdFrame)
        self.workOrderLabel.setGeometry(QtCore.QRect(10, 30, 75, 21))
        self.workOrderLabel.setObjectName("workOrderLabel")
        self.powerSupplyLabel1 = QtWidgets.QLabel(self.testIdFrame)
        self.powerSupplyLabel1.setGeometry(QtCore.QRect(10, 70, 101, 21))
        self.powerSupplyLabel1.setObjectName("powerSupplyLabel1")
        self.snTextEdit = QtWidgets.QTextEdit(self.testIdFrame)
        self.snTextEdit.setGeometry(QtCore.QRect(300, 20, 131, 31))
        self.snTextEdit.setStyleSheet("QTextEdit {\n"
"border: 1px solid black;\n"
"border-radius:5px; \n"
"}")
        self.snTextEdit.setObjectName("snTextEdit")
        self.voltageTextEdit1 = QtWidgets.QTextEdit(self.testIdFrame)
        self.voltageTextEdit1.setGeometry(QtCore.QRect(200, 60, 101, 31))
        self.voltageTextEdit1.setAutoFillBackground(False)
        self.voltageTextEdit1.setStyleSheet("QTextEdit {\n"
"border: 1px solid black;\n"
"border-radius:5px; \n"
"}")
        self.voltageTextEdit1.setObjectName("voltageTextEdit1")
        self.voltageLabel1 = QtWidgets.QLabel(self.testIdFrame)
        self.voltageLabel1.setGeometry(QtCore.QRect(120, 70, 71, 21))
        self.voltageLabel1.setObjectName("voltageLabel1")
        self.voltageLabel2 = QtWidgets.QLabel(self.testIdFrame)
        self.voltageLabel2.setGeometry(QtCore.QRect(120, 110, 71, 21))
        self.voltageLabel2.setObjectName("voltageLabel2")
        self.voltageTextEdit2 = QtWidgets.QTextEdit(self.testIdFrame)
        self.voltageTextEdit2.setGeometry(QtCore.QRect(200, 100, 101, 31))
        self.voltageTextEdit2.setAutoFillBackground(False)
        self.voltageTextEdit2.setStyleSheet("QTextEdit {\n"
"border: 1px solid black;\n"
"border-radius:5px; \n"
"}")
        self.voltageTextEdit2.setObjectName("voltageTextEdit2")
        self.powerSupplyLabel2 = QtWidgets.QLabel(self.testIdFrame)
        self.powerSupplyLabel2.setGeometry(QtCore.QRect(10, 110, 101, 21))
        self.powerSupplyLabel2.setObjectName("powerSupplyLabel2")
        self.currentLabel2 = QtWidgets.QLabel(self.testIdFrame)
        self.currentLabel2.setGeometry(QtCore.QRect(310, 110, 81, 21))
        self.currentLabel2.setObjectName("currentLabel2")
        self.currentLabel1 = QtWidgets.QLabel(self.testIdFrame)
        self.currentLabel1.setGeometry(QtCore.QRect(310, 70, 91, 21))
        self.currentLabel1.setObjectName("currentLabel1")
        self.currentTextEdit1 = QtWidgets.QTextEdit(self.testIdFrame)
        self.currentTextEdit1.setGeometry(QtCore.QRect(400, 60, 101, 31))
        self.currentTextEdit1.setAutoFillBackground(False)
        self.currentTextEdit1.setStyleSheet("QTextEdit {\n"
"border: 1px solid black;\n"
"border-radius:5px; \n"
"}")
        self.currentTextEdit1.setObjectName("currentTextEdit1")
        self.currentTextEdit2 = QtWidgets.QTextEdit(self.testIdFrame)
        self.currentTextEdit2.setGeometry(QtCore.QRect(400, 100, 101, 31))
        self.currentTextEdit2.setAutoFillBackground(False)
        self.currentTextEdit2.setStyleSheet("QTextEdit {\n"
"border: 1px solid black;\n"
"border-radius:5px; \n"
"}")
        self.currentTextEdit2.setObjectName("currentTextEdit2")
        self.verticalLayout.addWidget(self.testIdFrame)
        self.RXFrame = QtWidgets.QFrame(Form)
        self.RXFrame.setStyleSheet("background: transparent;")
        self.RXFrame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.RXFrame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.RXFrame.setObjectName("RXFrame")
        self.verticalLayout.addWidget(self.RXFrame)
        self.TXFrame = QtWidgets.QFrame(Form)
        self.TXFrame.setStyleSheet("background: transparent;")
        self.TXFrame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.TXFrame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.TXFrame.setObjectName("TXFrame")
        self.verticalLayout.addWidget(self.TXFrame)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.snLabel.setText(_translate("Form", "SN:"))
        self.workOrderLabel.setText(_translate("Form", "Work Order:"))
        self.powerSupplyLabel1.setText(_translate("Form", "Power Supply 1:"))
        self.voltageLabel1.setText(_translate("Form", "Voltage [V]:"))
        self.voltageLabel2.setText(_translate("Form", "Voltage [V]:"))
        self.powerSupplyLabel2.setText(_translate("Form", "Power Supply 2:"))
        self.currentLabel2.setText(_translate("Form", "Current [mA]:"))
        self.currentLabel1.setText(_translate("Form", "Current [mA]:"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

在我的 MainWindow class 中,我从新生成的 python 文件中导入 Ui_Form,该文件包含我想要复制的新选项卡的内容。 这就是我卡住的地方..我调用了将为我生成 uild 选项卡的函数:

def buildTabs(self, numberOfTabs): 
    for dut in range(numberOfTabs): 
        text = ('TAB ' + ascii_uppercase[dut]) 
        self.ui.tabWidget.addTab(Ui_Form(), text)

选项卡小部件位于主 window 的 ui 中。

我不明白如何将 Ui_Form() 变成 Ui_TabPage 或

中所示的 QWidget

QT 设计器窗体:

enter image description here

pyuic生成的文件是形式的classes,其实是标准的pythonobjectclasses,并且他们需要一个 QWidget 实例(或者在 Designer 中用于 ui 的任何 class,例如 QDialog)来“build”他们。

def buildTabs(self, numberOfTabs): 
    for dut in range(numberOfTabs): 
        page = QtWidgets.QWidget()
        page.ui = Ui_Form()
        page.ui.setupUi(page)        
        text = ('TAB ' + ascii_uppercase[dut])
        self.ui.tabWidget.addTab(page, text)

考虑到 PyQt 和 PySide 不能混用,所以如果您使用 PyQt,命令是 pyuic,而使用 PySide,命令是 pyside2-uic

请注意,如果您需要与选项卡 页面 进行进一步交互,通常最好将 class 分到它们:

class PageOne(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.ui = Ui_Form()
        self.ui.setupUi(self)

        self.ui.someButton.clicked.connect(self.someFunction)
        # ...

或者更好的是,使用多重继承方法,避免通过 ui“子对象”不断访问 ui 元素:

class PageOne(<b>QtWidgets.QWidget, Ui_Form</b>):
    def __init__(self, parent=None):
        super().__init__(parent)
        <b>self.setupUi(self)</b>

        <b>self.someButton.clicked.connect(self.someFunction)</b>
        # ...

然后:

def buildTabs(self, numberOfTabs): 
    for dut in range(numberOfTabs): 
        <b>page = PageOne()</b>
        text = ('TAB ' + ascii_uppercase[dut])
        self.ui.tabWidget.addTab(page, text)