Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. how to only show close button on the currently selected qtab widget? [pyqt, pyside]
Forum Updated to NodeBB v4.3 + New Features

how to only show close button on the currently selected qtab widget? [pyqt, pyside]

Scheduled Pinned Locked Moved Unsolved Qt for Python
4 Posts 3 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • adfinem_risingA Offline
    adfinem_risingA Offline
    adfinem_rising
    wrote on last edited by adfinem_rising
    #1

    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()
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      One way would be for you to add a button and show hide it when the tab changes.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      adfinem_risingA 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        One way would be for you to add a button and show hide it when the tab changes.

        adfinem_risingA Offline
        adfinem_risingA Offline
        adfinem_rising
        wrote on last edited by
        #3

        @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

        JonBJ 1 Reply Last reply
        0
        • adfinem_risingA adfinem_rising

          @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

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @adfinem_rising
          I don't quite follow about an "else". Your code only addresses the currentIndex() (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?

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved