How to hide a tab in QTabWidget and show it when a button is pressed

后端 未结 1 1127
梦如初夏
梦如初夏 2021-01-03 05:01

I have already created the tabs in QtabWidget. My main purpose is to hide tabs and show it when a button is pressed. I don\'t want to remove and add tabs every time. I want

相关标签:
1条回答
  • 2021-01-03 05:16

    There is no convenient method to hide a tab. You have two workarounds:

    1. Use removeTab and insertTab. You need to keep a reference to the tabs that you removed to be able to re-insert them later (and their indexes, so that they reappear on the same spot).

    2. Use setTabEnabled to enable (=show) and disabled (=hide) tabs. When a tab is disabled, it's usually grayed out, but you can use style sheets to hide the tab instead:

      self.setTabEnabled(tabIndex,True/False) #enable/disable the tab
      # set the style sheet
      self.setStyleSheet("QTabBar::tab::disabled {width: 0; height: 0; margin: 0; padding: 0; border: none;} ")
      

    I tested the second option, and had an issue when showing a previously hidden tab: the view was not repainted properly (even after calling update or repaint). Turns out the style sheet is not automatically updated and it still draws the tab as if it was disabled.
    Calling setStyleSheet after every setTabEnabled(index,True) fixes the issue. It forces the style sheet to be recompute.

    0 讨论(0)
提交回复
热议问题