0

i am trying to show the opened tab text in left .i wrote that in stylesheet.but it is not working.

here is the stylesheet i used

QTabWidget QTabBar{
    background-color: #373738;
    height: 30px;
}

QTabWidget QTabBar::tab{
    text-align: left;
    background-color: #136ba2;
    border-style: 1px rgb(67, 67, 67);
    height: 30px;
    width: 130px;
    color: #136ba2;
    padding: 0px 5px 0px 5px;
}

QTabWidget QTabBar::tab:selected{
    background-color: #5A5B5C;
    font-weight: bold;
    color: #ffffff;
}
QTabWidget QTabBar::tab:!selected{
    background-color:#353536;
    color:  #B4B4B4;
}
QTabWidget QTabBar::tab:hover{
    background-color:  #5A5B5C;
    color:  #ffffff;
}

here is an image

p-a-o-l-o
  • 9,807
  • 2
  • 22
  • 35
Shaber
  • 41
  • 1
  • 8

3 Answers3

1

I think it is not possible with stylesheet, according to the doc :

text-align [...] This property is currently supported only by QPushButton and QProgressBar.

https://doc.qt.io/qt-5/stylesheet-reference.html#list-of-properties

ymoreau
  • 3,402
  • 1
  • 22
  • 60
  • any other way to achieve this?? – Shaber Feb 14 '18 at 09:20
  • Others have asked without any answer from what I looked, it seems that there is nothing straightforward other than handling yourself the painting of the text. You can check the answers to this question : https://stackoverflow.com/q/3607709/6165833 – ymoreau Feb 14 '18 at 10:13
0

This can be accomplished using the tab button widget to show the tab text.

Have a label, with left aligned text:

QLabel * button = new QLabel("text");
button->setAlignment(Qt::AlignLeft);

Set the tab text to empty string if necessary:

ui->tabWidget->tabBar()->setTabText(index, "");

Set the label as the tab button widget:

ui->tabWidget->tabBar()->setTabButton(index, QTabBar::LeftSide, button);

You can still style the label, adding a QLabel entry in the tab widget stylesheet:

QLabel{ color: white; }

Only problem here: no way to style the label text depending on selection (i.e. making the selected tab text bold) using CSS. But still feasible catching the tab widget currentChanged signal:

void Form::on_tabWidget_currentChanged(int index)
{
    for(int i=0; i<ui->tabWidget->tabBar()->count(); i++)
    {
        QWidget * button = ui->tabWidget->tabBar()->tabButton(i, QTabBar::LeftSide);
        if(button != nullptr)
        {
            QFont font = button->font();
            font.setBold(i == index);
            button->setFont(font);
        }
    }
}
p-a-o-l-o
  • 9,807
  • 2
  • 22
  • 35
0

One workaround(not clean method) I found is add some extra space at the end of the title;

QString tabTitle = "Page 1     ";

Will move the text towards left.

Haris
  • 13,645
  • 12
  • 90
  • 121