[PyQt] Tab labels not shown correctly

Davide Liessi davide.liessi at gmail.com
Mon Mar 9 13:47:44 GMT 2020


Il giorno lun 9 mar 2020 alle ore 13:27 Souvik Dutta
<souvik.viksou at outlook.com> ha scritto:
> Did you use the correct selector like you should probably say,
> self.setStyleSheet(<object name>: disabled{padding: 0px 0px 0px 0px})

I started from the examples in
https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qtabwidget-and-qtabbar
and with the help of
https://doc.qt.io/qt-5/stylesheet-reference.html
I arrived at the example below, which reproduces quite well the
default style on Mac, while also fixing the display of the text in the
tabs.

Thanks for your help!
Best wishes.
Davide

Example:

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QMainWindow, QVBoxLayout, QWidget,
QApplication, QTabBar)
from PyQt5.QtGui import QIcon

qApp = QApplication([])
win = QMainWindow()

win.tabBar = QTabBar(win)
win.tabBar.setExpanding(False)
win.tabBar.setElideMode(Qt.ElideNone)
win.tabBar.setTabsClosable(True)
win.tabBar.addTab(QIcon('text-plain.svg'), 'Untitled')
win.tabBar.addTab(QIcon('text-plain.svg'), 'Untitled 2')
win.tabBar.addTab(QIcon('text-plain.svg'), 'Untitled 3')
win.tabBar.addTab(QIcon('text-plain.svg'), 'Untitled 4')
win.tabBar.addTab(QIcon('text-plain.svg'), 'Untitled 5')

style = """
QTabBar::tab {
    background: white;
    border-style: solid;
    border-width: 1px 0px;
    border-color: #ACACAC;
    min-width: 8ex;
    padding: 2px 4px 2px 2px;
}

QTabBar::tab:selected:active {
    border-color: #045FFF;
    background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                stop: 0 #69B1FA, stop: 1 #0C80FF);
    color: white;
}

QTabBar::tab:selected:!active {
    border-color: #ACACAC;
    background: #E5E5E5;
}

QTabBar::tab:first {
    border-left-width: 1px;
    border-top-left-radius: 4px;
    border-bottom-left-radius: 4px;
}

QTabBar::tab:!first:!selected:!previous-selected {
    border-left-color: #E5E5E5;
    border-left-width: 1px;
}

QTabBar::tab:last {
    border-right-width: 1px;
    border-top-right-radius: 4px;
    border-bottom-right-radius: 4px;
}
"""

win.tabBar.setStyleSheet(style)

layout = QVBoxLayout()
layout.addWidget(win.tabBar)

mainwidget = QWidget()
win.setCentralWidget(mainwidget)
mainwidget.setLayout(layout)

win.show()
qApp.exec_()


More information about the PyQt mailing list