Dennis
Dennis

Reputation: 269

Color the Non selected tabs in a QTabWidget

enter image description here

In a 3-tab QTabWidget, I would like to change the background color of the NON-SELECTED tabs, leaving the selected tab in the default background color (white). In other words, I want to change the color of the small part of the two non-selected tabs that shows above the selected tab.

I've looked all around this site and have found tid bits of code, but I cannot translate them into the format I need. So far,I have this in the init of my program, but it is incomplete and does not work.

self.ui.tabWidget.tabBar().setStyleSheet('xxxxxxxx {background-color: yellow }')

Upvotes: 1

Views: 1720

Answers (1)

eyllanesc
eyllanesc

Reputation: 243965

You have to use the QTabBar::tab:!selected:

from PyQt5 import QtWidgets

if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = QtWidgets.QTabWidget()

    # For more information to customize the QTabWidget and QTabBar check
    # https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qtabwidget-and-qtabbar
    w.setStyleSheet(
        """
    QTabBar::tab:!selected {
        background: green
    }
    """
    )
    for i in range(5):
        w.addTab(QtWidgets.QWidget(), f"tab-{i}")
    w.show()
    w.resize(640, 480)
    sys.exit(app.exec_())

In your case:

self.ui.tabWidget.setStyleSheet("""QTabBar::tab:!selected{ background: green }""")

Upvotes: 4

Related Questions