PyQT Qtabwidget 隐藏和关闭某些选项卡 [英] PyQT Qtabwidget hide and close certain tab

查看:659
本文介绍了PyQT Qtabwidget 隐藏和关闭某些选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 Qtabwidget 中隐藏和关闭某个选项卡?我有 5 个选项卡,其中两个是在使用我的软件时生成的图.首先,我想在开始时隐藏两个图,其次我想让它们在生成后关闭.这可能吗?使用 self.setTabsClosable(True) 所有标签都可以关闭.

Is it possible to hide and close a certain tab in Qtabwidget? I have 5 tabs, two of them are plots and generated while using my software. First I want to hide the two plots in the beginning and second I want to make them closable after there were generated. Is this possible? With self.setTabsClosable(True) all tabs will be closable.

谢谢

import sys
from PyQt4 import QtGui

class QCustomTabWidget (QtGui.QTabWidget):
    def __init__ (self, parent = None):
        super(QCustomTabWidget, self).__init__(parent)
        self.setTabsClosable(True)
        self.tabCloseRequested.connect(self.closeTab)
        for i in range(1, 10):
            self.addTab(QtGui.QWidget(), 'Tab %d' % i)

    def closeTab (self, currentIndex):
        currentQWidget = self.widget(currentIndex)
        currentQWidget.deleteLater()
        self.removeTab(currentIndex)

myQApplication = QtGui.QApplication([])
myQCustomTabWidget = QCustomTabWidget()
myQCustomTabWidget.show()
sys.exit(myQApplication.exec_())

推荐答案

您可以移除不应该关闭的选项卡的关闭按钮.这是通过QTabBarsetTabButton函数来完成的,就像这样:

You can remove the close button of the tabs that should not be closed. This is done by with the function setTabButton of QTabBar, like this:

QtGui.QTabWidget.tabBar().setTabButton(0, QtGui.QTabBar.RightSide,None)

这里,我们将第一个标签页的按钮设置为None.

Here, we set the button of the first tab to be None.

使用相同的功能,您还可以在选项卡上创建自己的关闭按钮(并删除 self.setTabsClosable(True))

With the same function, you could also create your own close button on a tab (and remove self.setTabsClosable(True))

这篇关于PyQT Qtabwidget 隐藏和关闭某些选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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