QTabWidget with CheckBox in title

I was wondering how to create (using PyQt4) a derived QTabWidget with a checkbox next to each bookmark title? Like this:

+4
source share
2 answers

In fact, I only selected a subclass of QTabWidget.
A CheckBox is added when a new tab is created and stored in the list to return its index.
The setCheckState / isChecked method is used to control the state of each checkBox indicated by the tab index.
Finally, the stateChanged (int) signal is captured and sent with an additional parameter defining the index of the corresponding control panel.

 class CheckableTabWidget(QtGui.QTabWidget): checkBoxList = [] def addTab(self, widget, title): QtGui.QTabWidget.addTab(self, widget, title) checkBox = QtGui.QCheckBox() self.checkBoxList.append(checkBox) self.tabBar().setTabButton(self.tabBar().count()-1, QtGui.QTabBar.LeftSide, checkBox) self.connect(checkBox, QtCore.SIGNAL('stateChanged(int)'), lambda checkState: self.__emitStateChanged(checkBox, checkState)) def isChecked(self, index): return self.tabBar().tabButton(index, QtGui.QTabBar.LeftSide).checkState() != QtCore.Qt.Unchecked def setCheckState(self, index, checkState): self.tabBar().tabButton(index, QtGui.QTabBar.LeftSide).setCheckState(checkState) def __emitStateChanged(self, checkBox, checkState): index = self.checkBoxList.index(checkBox) self.emit(QtCore.SIGNAL('stateChanged(int, int)'), index, checkState) 

This may not be the perfect way to do something, but at least it remains fairly simple and covers all my needs.

+8
source

The best approach is

  • Create a custom TabBar, CheckedTabBar (inheriting QTabBar )
  • Create a custom TabWidget, CheckedTabWidget (inheriting QTabWidget )
  • Add a check method if the checkbox is checked or not, and maybe some signals when the flag is switched :)

You must set your own tab in the checktabwidget constructor, for example:

 CheckedTabWidget::CheckedTabWidget(QWidget* parent) : QTabWidget(parent) { setTabBar(new CheckedTabBar(this)); } 
+2
source

All Articles