Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. The tab-text is out of range when I set the QTabBar::tab text to bold
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 3.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • LimerL Offline
    LimerL Offline
    Limer
    wrote on last edited by
    #1

    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?

    JonBJ 2 Replies Last reply
    0
    • LimerL Limer

      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?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @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.

      1 Reply Last reply
      0
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #3

        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

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        1
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          4
          • LimerL Limer

            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?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @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; }
            
            LimerL 1 Reply Last reply
            0
            • JonBJ JonB

              @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; }
              
              LimerL Offline
              LimerL Offline
              Limer
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved