Reputation: 183
I am creating a QTabWidget, and I set True to its setTabsClosable method, so that each tab has a close button. The effect I want is that the first tab has no close button, and the second button has a close button. How should I set it?
from PyQt4.QtGui import QTabWidget, QLabel, QHBoxLayout
import sys
import sys
from PyQt4 import QtGui
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def create_tab(self):
self.tab = QTabWidget()
self.tab.tabCloseRequested.connect(self.delete)
self.tab.setTabsClosable(True)
self.tab.addTab(QLabel('a'), 'a')
self.tab.addTab(QLabel('b'), 'b')
def initUI(self):
self.create_tab()
h = QtGui.QHBoxLayout()
self.setLayout(h)
h.addWidget(self.tab)
self.setGeometry(100, 100, 500, 500)
self.show()
def delete(self, index):
self.tab.removeTab(index)
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Upvotes: 3
Views: 928
Reputation: 244013
One possible solution is to remove the buttons after setting the tabs:
def create_tab(self):
self.tab = QtGui.QTabWidget()
self.tab.tabCloseRequested.connect(self.delete)
self.tab.setTabsClosable(True)
self.tab.addTab(QtGui.QLabel("a"), "a")
self.tab.addTab(QtGui.QLabel("b"), "b")
default_side = self.tab.style().styleHint(
QtGui.QStyle.SH_TabBar_CloseButtonPosition, None, self.tab.tabBar()
)
for i in (0,): # indexes of the buttons to remove
self.tab.tabBar().setTabButton(i, default_side, None)
Upvotes: 2