QTabWidget style sheet



  • Hi
    I have a style sheet for QTabWidget and QTabBar that looks ok except that I cannot figure out how to make the tab width dependant on the text that it is displaying. Currently I have this as a fixed value but when I add a new tab with a wider label I have to adjust the style sheet.
    Is there a way of doing this dynamically?

    here is my style sheet
    @
    QTabWidget, QStackedWidget
    {
    background: none
    }
    QTabWidget QWidget
    {
    background-color: lightblue;
    }

    QTabWidget::pane
    {
    border: 0px;
    top: 0.1em;
    border-image: url(tabBG.png);
    border-width: 1px;
    border-style: solid;
    border-color: grey;
    }
    QTabBar::tab
    {
    width: 120px;
    margin-left: 0.5;

    margin-right: 0.5;
    
     border: 1px solid #7a7a7a;
    
     border-top-left-radius: 4px;
    border-top-right-radius: 4px;
    padding: 2px 6px 2px 6px;
    

    }

    QTabBar::tab:!selected {
    margin-top: 2px;
    }
    QTabBar::tab:selected {
    margin-top: -1px;
    background-color: lightblue;
    borderwidth: 0px;
    }

    QTabBar::tab::bottom
    {
    border-bottom-left-radius: 4px;
    border-bottom-right-radius: 4px;
    border-top-left-radius: 0px;
    border-top-right-radius: 0px;
    padding: 2px 6px 2px 6px;
    }
    @



  • Hi,

    I am not sure you could achieve your goal using style sheets. I think reimplementing one of the methods used by Qt to compute the size of each widget and return a size based on the QTabBar text could do the trick (e.g. sizeHint or minimumSizeHint).



  • By setting a width for the tabs I believe they will be fixed at that width, preventing them from adjusting automatically to fit the text.

    @QTabBar::tab
    {
    width: 120px;@

    As a suggestion, try commenting out that line and see what happens.

    You can of course use the min-width property if you want, well, a minimum width.

    Just a thought.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.