QSS QTabBar text-align does not work

1.7k views Asked by At

I try (font-weigth: works, but text-align: does not work):

QTabBar::tab:text{
    font-weight:bold;
    text-align:left;
}

I try, but it was also not working:

QTabBar::tab {
    font-weight:bold;
    text-align:left;
}

My QTabWidget:

<item>
 <widget class="QTabWidget" name="tabWidget">
  <widget class="QWidget" name="tab">
   <attribute name="title">
    <string>Tab 1</string>
   </attribute>
  </widget>
  <widget class="QWidget" name="tab_2">
   <attribute name="title">
    <string>Tab 2</string>
   </attribute>
  </widget>
 </widget>
</item>

I also have not found any "property" (<property name="...">) that aligns the text.

how to use the "QSS" to align the text of the tabs (QTabBar/QTabWidget) ?

3

There are 3 answers

2
MozzieJoe On

I do not think this is supported in the current version of Qt (5.4). You can try using a paint event with QPainter to draw the text onto the tab in the alignment you want.

You could use specific padding and Qt::AlignLeft to get this outcome potentially.

See this post on the Qt forum for more info - https://forum.qt.io/topic/35405/qss-qtabbar-text-align-does-not-work

0
elsamuko On

As a workaround you can pad the title with spaces at the end, use setElideMode(Qt::ElideRight) and use a custom QTabBar with reimplemented sizeHint() to avoid stretched tabs:

QSize TabBar::tabSizeHint(int index) const {
    QSize size = QTabBar::tabSizeHint(index);
    size.setWidth(250);
    return size;
}
0
June On

Try setElideMode(Qt::ElideRight);