Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

The tab-text is out of range when I set the QTabBar::tab text to bold



  • 0_1542811394612_8.gif

    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 than min-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 default QWidget::fontMetrics to calculate it instead of the styled one.

    Possible solution

    in QTabBar::tabSizeHint the line it = d->textSizes.insert(tab->text, fm.size(Qt::TextShowMnemonic, tab->text)); should not use fm but instead should call QTabBar::initStyleOption(QStyleOptionTab *option, int tabIndex) and use option->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 used

    Edit: no, the solution is more complicated than that


  • Lifetime Qt Champion

    This is sadly a long-outstanding issue: https://bugreports.qt.io/browse/QTBUG-6905



  • @Limer
    In the link @Christian-Ehrlicher has quoted, there is a workaround which claims to, well, work around your issue (as best it can):

    QTabBar {  font-weight: bold;  }
    QTabBar::tab:!selected { font-weight: normal; }
    


  • @JonB Yes , you are right. I make it! Thanks very much for you, @JonB @Christian-Ehrlicher @VRonin


Log in to reply