I used this question/answer (pyqt5 tabwidget vertical tab horizontal text alignment left) to position the tabs vertically and text horizontal but I am unable to move the tabButton (QCheckBox) that I am adding from the center of the text.
class TabBar(QtWidgets.QTabBar):
def tabSizeHint(self, index):
s = QtWidgets.QTabBar.tabSizeHint(self, index)
s.transpose()
return s
def paintEvent(self, event):
painter = QtWidgets.QStylePainter(self)
opt = QtWidgets.QStyleOptionTab()
for i in range(self.count()):
self.initStyleOption(opt, i)
painter.drawControl(QtWidgets.QStyle.CE_TabBarTabShape, opt)
painter.save()
s = opt.rect.size()
s.transpose()
r = QtCore.QRect(QtCore.QPoint(), s)
r.moveCenter(opt.rect.center())
opt.rect = r
c = self.tabRect(i).center()
painter.translate(c)
painter.rotate(90)
painter.translate(-c)
painter.drawControl(QtWidgets.QStyle.CE_TabBarTabLabel, opt)
painter.restore()
class CheckableTabWidget(QtWidgets.QTabWidget):
"""
Creates a clickable tab widget.
"""
checked_list = []
def __init__(self, *args, **kwargs):
QtWidgets.QTabWidget.__init__(self, *args, **kwargs)
self.setTabBar(TabBar(self))
self.setTabPosition(QtWidgets.QTabWidget.West)
def addTab(self, widget, title):
"""
Add a tab to the tab bar.
:param widget: the widget to add to the tab bar.
:param title: string to display on the tab bar.
"""
QtWidgets.QTabWidget.addTab(self, widget, title)
checkbox = QtWidgets.QCheckBox()
self.checked_list.append(checkbox)
self.tabBar().setTabButton(self.tabBar().count()-1,
QtWidgets.QTabBar.LeftSide, checkbox)
How it currently looks: ie. checkbox on top of text
Ideally, I would like it to be to the left or right of the text.