0

A MRE to customize QTabWidget tabs reads:

import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QLabel, QWidget, QAction, QTabWidget,QVBoxLayout
from PyQt5.QtCore import pyqtSlot

class App(QMainWindow):

    def __init__(self):
        super().__init__()
        self.title = 'Tabs tailoring'
        self.setWindowTitle(self.title)
        self.table_widget = TailoredWidget(self)
        self.setCentralWidget(self.table_widget)
        self.show()

class TailoredWidget(QWidget):

    def __init__(self, parent):
        super(QWidget, self).__init__(parent)
        self.layout = QVBoxLayout(self)
        # Initialize tab screen
        self.tabs = QTabWidget()
        self.tabs.setStyleSheet('''QTabBar::tab {font-size: 20pt; color: #00004F; height: 40px; width: 140px;}''')
        self.tab1 = QWidget()
        self.tab2 = QWidget()
        self.tab3 = QWidget()
        # Add tabs
        self.tabs.addTab(self.tab1,"TAB 1")
        self.tabs.addTab(self.tab2,"TAB 2")
        self.tabs.addTab(self.tab3,"TAB 3")
        # Add tabs to widget
        self.layout.addWidget(self.tabs)
        self.setLayout(self.layout)

    @pyqtSlot()
    def on_click(self):
        print("\n")
        for currentQTableWidgetItem in self.tableWidget.selectedItems():
            print(currentQTableWidgetItem.row(), currentQTableWidgetItem.column(), currentQTableWidgetItem.text())

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_()) 

This works as expected and allows to play around with the setStyleSheet parameters. However, I'd prefer to be able to customize each tab individually. I tried modifying the initialization of the tabs to:

        # Initialize tab screen
        self.tabs = QTabWidget()
#        self.tabs.setStyleSheet('''QTabBar::tab {font-size: 30pt; color: #00004F; height: 40px; width: 140px;}''')
        self.tab1 = QWidget()
        self.tab1.setStyleSheet('''QTabBar::tab {font-size: 15pt; color: #55004F; height: 40px; width: 140px;}''')
        self.tab2 = QWidget()
        self.tab2.setStyleSheet('''QTabBar::tab {font-size: 20pt; color: #AA004F; height: 40px; width: 140px;}''')
        self.tab3 = QWidget()
        self.tab3.setStyleSheet('''QTabBar::tab {font-size: 25pt; color: #FF004F; height: 40px; width: 140px;}''')

but this doesn't do anything. Is there any way to assign each tab different properties?

  • 1
    No, you cannot using stylesheets. The only available customization for each tab is through [`setTabTextColor()`](https://doc.qt.io/qt-5/qtabbar.html#setTabTextColor) of the QTabBar. The only alternative is to **completely** draw everything on your own by overriding the `paintEvent()` of the tab bar and using QStyle features. – musicamante Oct 14 '22 at 13:11
  • You should also be aware that some styles draw tabs using pixmaps, so you can't change the colours via the palette in the paint-event. See this Qt FAQ: [Why does nothing happen when I set the palette background color](https://wiki.qt.io/Technical_FAQ#Why_does_nothing_happen_when_I_set_the_palette_background_color_on_a_QPushButton_in_some_styles.3F). If you're not affected by this issue, see [this answer](https://stackoverflow.com/a/12903597/984421) for a potential solution. – ekhumoro Oct 14 '22 at 14:22
  • Hi @ekhumoro. I tried the PyQt5 in an Ubuntu22.04 system with Gnome, and it colored the border of the tabs with the different colors, but the tab background stayed white. – afernandezody Oct 14 '22 at 21:38
  • @afernandezody Try editing the PyQt5 example and adding the line `option.palette.setColor(QtGui.QPalette.Button, bgcolor)`. You may need to experiment with the [palette colors](https://doc.qt.io/qt-5/qpalette.html#ColorRole-enum) to get the right results, since each widget-style may behave differently. – ekhumoro Oct 14 '22 at 22:15

0 Answers0