用于在 QTabWidget 中复制选项卡的按钮 [英] Button for duplicating tabs in a QTabWidget

查看:97
本文介绍了用于在 QTabWidget 中复制选项卡的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,它使用带有多个输入框的输入选项卡供用户填写,当这些输入框被填写时,我点击运行,另一个程序将运行输入的数据.我希望我的程序有一个带有加号的选项卡按钮,可以动态复制选项卡,以便我可以运行多个实例.我希望每个标签都使用相同的名称,但最后有一个变体 - 例如EntryBox0"、EntryBox1"、EntryBox2".

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.

我想在Tab 2"旁边添加 + 号.所有选项卡都相同.小部件将具有相同的名称,但末尾有一个动态编号,用于标识正在使用的选项卡.

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_())

推荐答案

QTabWidget 类有一个 setCornerWidget 方法,可用于向标签栏添加按钮.这只能位于选项卡栏的左端或右端(而不是最后一个选项卡之后) - 但是,对我来说,这似乎对用户更友好,因为当新选项卡出现时按钮不会移动添加.通过为标签页创建一个单独的类,标签本身可以很容易地复制.

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:

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_())

这篇关于用于在 QTabWidget 中复制选项卡的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆