how to only show close button on the currently selected qtab widget? [pyqt, pyside]
-
I want to be able to only show the close icon on the current active tab and the rest of the other open tabs have their close icons be hidden.
this is the code so far:
import sys from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5.QtGui import * class Example(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.currentChanged.connect(self.show_close_button) self.tab.setTabsClosable(True) self.tab.addTab(QLabel("a"), "New Tab1") self.tab.addTab(QLabel("b"), "New Tab2") def show_close_button(self): default_side = self.tab.style().styleHint(QStyle.SH_TabBar_CloseButtonPosition, None, self.tab.tabBar()) for i in (self.tab.currentIndex(),): # indexes of the buttons to remove self.tab.tabBar().setTabButton(i, default_side, None) def initUI(self): self.create_tab() h = 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 = QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) if __name__ == '__main__': main() -
Hi,
One way would be for you to add a button and show hide it when the tab changes.
-
@SGaist yes, that's also possible however it wouldn't look seamless. but what i really want is to know how to put together the if else that would hide all the other close icons when they are not the currently selected tab.
def show_close_button(self): default_side = self.tab.style().styleHint(QStyle.SH_TabBar_CloseButtonPosition, None, self.tab.tabBar()) for i in (self.tab.currentIndex(),): # indexes of the buttons to remove self.tab.tabBar().setTabButton(i, default_side, None)this code removes the close icon of the tab that i clicked. i kinda want it to be reversed. i think im close, i just can't realize how to construct my if else condition
-
@SGaist yes, that's also possible however it wouldn't look seamless. but what i really want is to know how to put together the if else that would hide all the other close icons when they are not the currently selected tab.
def show_close_button(self): default_side = self.tab.style().styleHint(QStyle.SH_TabBar_CloseButtonPosition, None, self.tab.tabBar()) for i in (self.tab.currentIndex(),): # indexes of the buttons to remove self.tab.tabBar().setTabButton(i, default_side, None)this code removes the close icon of the tab that i clicked. i kinda want it to be reversed. i think im close, i just can't realize how to construct my if else condition
@adfinem_rising
I don't quite follow about an "else". Your code only addresses thecurrentIndex()(and for some reason makes a 1-element list out of it). Why don't you just visit all the tabs, and do something different to the non-current-index ones?