0

Is there any way to change the height of the tabs on a QToolBox widget and center the title?

I am able to change the height by setting tabSpacing, but then the title is not centered (it is top-aligned). I tried changing the alignment with the stylesheet but no luck. I also tried to force the height with:

QToolBox::tab {
     height: 48px;
     alignment: center;"
     margin: 0px 0px 0px 0px;"
}

BUt it is not working either. Any ideas?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
laurapons
  • 971
  • 13
  • 32
  • The alignment property is called text-align, but "This property is currently supported only by QPushButton and QProgressBar." (From documentation). Height also does not appear work. The answer to sylesheet questions usually is: It is not possible. What you can try is deriving a class from QProxyStyle and override pixelMetric() to return the desired height and do something similar for the alignment. – SteakOverflow Jan 30 '18 at 10:26
  • I should note that if you try to create a heavily styled application, you will have to use a custom style (QProxyStyle) sooner or later, thus I would not spend too much time on style sheets in that case. – SteakOverflow Jan 30 '18 at 10:28
  • I know there is the controlElement CE_ToolBoxTab, but I am not sure how to handle it with proxys... Can you help me on that? – laurapons Feb 15 '18 at 11:13
  • You only need CE_ToolBoxTab if you want to draw it yourself. If you just want to change some dimensions, use pxelMetric(), as I suggested earlier. Read [Creating a Custom Style](http://doc.qt.io/qt-5/qstyle.html#creating-a-custom-style), [QStyle::pixelMetric](http://doc.qt.io/qt-5/qstyle.html#pixelMetric) and [QStyle::PixelMetric](http://doc.qt.io/qt-5/qstyle.html#PixelMetric-enum). My guess is PM_ToolBarHandleExtent or PM_TabBarBaseHeight should work. I did not try it though, therefore this was a comment, not an answer. – SteakOverflow Feb 15 '18 at 12:13
  • It doesn't work, I created a separate question to track it, you can see the details there https://stackoverflow.com/questions/48805982/change-height-and-icon-position-of-qtoolbox-tabs-using-proxys – laurapons Feb 15 '18 at 12:47

2 Answers2

1

After speaking with Qt support, the only way I found was to assign an empty icon to all tabs, and set the icon-size to the desired height.

...
for (int i = 0; i < ui->toolBox->count(); i++){
    ui->toolBox->setItemIcon( i, QIcon(":/shared/empty"));
}
ui->toolBox->setStyleSheet("QToolBox{ icon-size: 48px; }");
...
laurapons
  • 971
  • 13
  • 32
0

For Qt6 you could try to use:

QToolBoxButton {
    height: 48px;
    /*font-size: 16px;*/
}
Fabian Keßler
  • 563
  • 3
  • 12