Button for duplicating tabs in a QTabWidget

戏子无情 提交于 2021-01-28 05:09:07

问题


I have a program that uses an input tab with multiple entry boxes for a user to fill out, and when those entry boxes are filled in, I hit run and another program will run the data entered. I'd like for my program to have a tab button with a plus sign that will duplicate the tabs dynamically so that I can run multiple instances. I'd like for each tab to use the same name, but have a variant at the end - e.g. "EntryBox0", "EntryBox1", "EntryBox2".

Is there an easy way to do this, while keeping the formatting (widget positions) the same in all tabs?

My first thought was to code the multiple tabs manually and have a hide/unhide button for each tab, however that would be a lot more code.

enter image description here

I'd like to add the + sign next to "Tab 2". All of the tabs will identical. The widgets will have same name, but have a dynamic number at the end to identify which tab is being used.


from Stage import Ui_Form
from Tabs import Ui_TabPage

class TabPage(QWidget, Ui_TabPage):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setupUi(self)

class Stage(QMainWindow, Ui_Form):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setupUi(self)
        button = QtWidgets.QToolButton()
        button.setToolTip('Add New Tab')
        button.clicked.connect(self.addNewTab)
        button.setIcon(self.style().standardIcon(
            QtWidgets.QStyle.SP_DialogYesButton))
        self.tabWidget.setCornerWidget(button, QtCore.Qt.TopRightCorner)
        self.addNewTab()

    def addNewTab(self):
        text = 'Tab %d' % (self.tabWidget.count() + 1)
        self.tabWidget.addTab(TabPage(self.tabWidget), text)

if __name__ == '__main__':

    app = QtWidgets.QApplication(sys.argv)
    window = Stage()
    window.setGeometry(600, 100, 300, 200)
    window.show()
    sys.exit(app.exec_())

回答1:


The QTabWidget class has a setCornerWidget method that can be used to add a button to the tab-bar. This can only be positioned at either the left or right end of the tab-bar (rather than after the last tab) - but, to me, that seems more user-friendly, since the button won't move when a new tab is added. The tabs themselves can be easily duplicated by creating a separate class for the tab page.

Below is a simple demo script that implements that:

screenshot

import sys
from PyQt5 import QtCore, QtWidgets

class TabPage(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        group = QtWidgets.QGroupBox('Monty Python')
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(group)
        grid = QtWidgets.QGridLayout(group)
        grid.addWidget(QtWidgets.QLabel('Enter a name:'), 0, 0)
        grid.addWidget(QtWidgets.QLabel('Choose a number:'), 0, 1)
        grid.addWidget(QtWidgets.QLineEdit(), 1, 0)
        grid.addWidget(QtWidgets.QComboBox(), 1, 1)
        grid.addWidget(QtWidgets.QPushButton('Click Me!'), 1, 2)
        grid.addWidget(QtWidgets.QSpinBox(), 2, 0)
        grid.addWidget(QtWidgets.QPushButton('Clear Text'), 2, 2)
        grid.addWidget(QtWidgets.QTextEdit(), 3, 0, 1, 3)

class Window(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.tabs = QtWidgets.QTabWidget()
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.tabs)
        button = QtWidgets.QToolButton()
        button.setToolTip('Add New Tab')
        button.clicked.connect(self.addNewTab)
        button.setIcon(self.style().standardIcon(
            QtWidgets.QStyle.SP_DialogYesButton))
        self.tabs.setCornerWidget(button, QtCore.Qt.TopRightCorner)
        self.addNewTab()

    def addNewTab(self):
        text = 'Tab %d' % (self.tabs.count() + 1)
        self.tabs.addTab(TabPage(self.tabs), text)

if __name__ == '__main__':

    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    window.setGeometry(600, 100, 300, 200)
    window.show()
    sys.exit(app.exec_())


来源:https://stackoverflow.com/questions/47556122/button-for-duplicating-tabs-in-a-qtabwidget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!