Qt4 expands tabs in QTabBar

I will subclass QTabWidget to add a QTabBar whose tabs stretch across the width of the tabBar . Therefore, I set the expand property to true. This does not seem to change anything about the behavior of tabs.

Has anyone encountered the same problem? I am using Qt 4.6 in conjunction with

 TabWidget::TabWidget(QWidget *parent) { tabBar = new QTabBar(this); tabBar->setIconSize(QSize(160,160)); tabBar->setExpanding(true); setTabBar(tabBar); } 

EDIT:, here's how I implemented it, in case anyone is interested:

  tabBar = new QTabBar(this); tabBar->setExpanding(true); layout = new QVBoxLayout(this); setLayout(layout); stackedLayout = new QStackedLayout(); layout->addWidget(tabBar); layout->addLayout(stackedLayout); connect(tabBar, SIGNAL(currentChanged(int)), stackedLayout, SLOT(setCurrentIndex(int))); void MainWindow::addTab(QWidget *widget, const QIcon &icon, const QString &label) { tabBar->addTab(icon, label); stackedLayout->addWidget(widget); } 
+6
qt qt4 qtabwidget qtabbar
source share
3 answers

From the source code of QTabBar :

 // ... Since we don't set // a maximum size, tabs will EXPAND to fill up the empty space. // Since tab widget is rather *ahem* strict about keeping the geometry of the // tab bar to its absolute minimum, this won't bleed through, but will show up // if you use tab bar on its own (aka not a bug, but a feature). 

To get around this “feature”, you can create your own tab widget using the QTabBar above the widgets with QStackedLayout .

+3
source share

Based on @baysmith's answer , an easier way to get QTabWidget to enable QTabBar expand is to set the stylesheet to QTabWidget which looks something like this:

 QTabWidget::tab-bar { width: 999999px; } 

Or another ridiculously large number. If your QTabWidget has tabs going vertically rather than horizontally, use "height" instead:

 QTabWidget::tab-bar { height: 999999px; } 

This seems to work fine for me, with Qt 5.0.1. Tabs expand to fill the gap, each of which receives an equal share. However, it looks like they intentionally leave enough free space for another tab, independently. But the rest of the space is filled at will. Empty space can be reserved for tear / scroll buttons if you add too many tabs, but I'm not sure.

0
source share

5.2.0 onwards

 QTabWidget::tab-bar { min-width: 999999px; } 

That will work. No need to use any combination. You can use QTabWidget. Daniel Anse is right.

0
source share

All Articles