The tab-text is out of range when I set the QTabBar::tab text to bold
-
qss code:
QTabBar::tab { background: rgb(235, 236, 237); border: 1px solid rgb(179, 179, 179); border-bottom: none; border-top-left-radius: 4px; border-top-right-radius: 4px; padding: 6px; /* min-width: 100px; // but it didn't work. */ margin-left: 3px; } QTabBar::tab:selected { font-weight: bold; padding: 4px; border-left: none; border-top: none; border-bottom: none; background-color: white; }
Although I set the tab-width, it didn't work.
Can someone give me any advice?
-
@Limer
I don't know, and I don't know whether you should be needing to set the tab width anyway or whether it should auto-cope with your change to bold text.However, there appear to be quite a few web posts out there claiming difficulty with tabs not responding to CSS.
For example, https://stackoverflow.com/questions/26045252/qtabbar-tab-size-doesnt-scale-with-stylesheet-font seems to be just the same case as you. Also https://stackoverflow.com/questions/14124787/how-can-i-set-qtabwidgets-tab-bars-length. Have a read.
One admittedly old post https://stackoverflow.com/a/4169468/489865 claims:
Setting QTabBar::tab { width: 100px; height: 20px; } in CSS did not work, even though other values were affected by CSS (background color, etc.).
Setting it programatically worked:
tabWidget->setStyleSheet("QTabBar::tab { width: 100px; height: 20px; }");
If it were me, I'd give that a try. Even if it's not a solution for you, it might confirm what will/will not work. I'd also the CSS with a much larger width than 100px (I can't even tell for sure whether 100px is enough for the example you show, try 500px instead then we'll be sure!), and I'd try plain
width
rather thanmin-width
. -
It's a bug and needs changes to the Qt internals to be solved.
Explanation
QTabBar::tabSizeHint
calculates the size text size and stores it in a container so that it doesn't get recalculated every time. The problem is that it uses the defaultQWidget::fontMetrics
to calculate it instead of the styled one.Possible solution
inQTabBar::tabSizeHint
the lineit = d->textSizes.insert(tab->text, fm.size(Qt::TextShowMnemonic, tab->text));
should not usefm
but instead should callQTabBar::initStyleOption(QStyleOptionTab *option, int tabIndex)
and useoption->fontMetrics
. I'm unsure however on how to force that method to use the:selected
part of the QSS so that the maximum between the normal and the selected one can be usedEdit: no, the solution is more complicated than that
-
This is sadly a long-outstanding issue: https://bugreports.qt.io/browse/QTBUG-6905