Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to expand tabs in QTabWidget Qt

Tags:

c++

qt

qtabwidget

I have a QTabWidget like this one:

enter image description here

But I want to expand the tabs to "fill" the entire widget width, like this:

enter image description here

How can I do that?

I am using Qt 5.3.2 and Qt Creator 3.2.1


Update:

I tried to use the setExpanding function:

ui->myTabWidget->tabBar()->setExpanding(true);

But it didn't work.

like image 744
KelvinS Avatar asked Feb 15 '17 14:02

KelvinS


People also ask

How do I use tab widget in Qt?

The normal way to use QTabWidget is to do the following: Create a QTabWidget. Create a QWidget for each of the pages in the tab dialog, but do not specify parent widgets for them. Insert child widgets into the page widget, using layouts to position them as normal.

How do I add a tab in PyQt?

PyQt tabs example To add a tab to a QTabWidget , call the method . addTab() . label1 = QLabel("Widget in Tab 1.") label2 = QLabel("Widget in Tab 2.")


1 Answers

I found that QTabBar has a setExpanding method, which appears to do exactly what you want, but I tried it (on Windows), and it doesn't work. This is the code:

ui->tabWidget->tabBar()->setExpanding (true);

Then I found the following post:

https://forum.qt.io/topic/47404/qtabbar-will-not-expand-its-tabs

I find the answer provided in the above post to be debatable. He says it's respecting the operating system style whether or not the expanding property is set to true and that it's a feature, not a bug, and that you have to subclass QTabBar to get the desired behavior. If I write code to do a specific thing, I feel like my instructions should override the OS style. If I just wanted the OS style, I would leave that special code out. However much I disagree with the implementation, that appears to be what we're stuck with.

So if it's important to you to have this look, then you'll need to subclass QTabBar, override the tabSizeHint--I suspect that will take some experimentation--and use QTabWidget::setTabBar to replace the default with your own. According to the documentation, you have to do that before adding any tabs, so this mechanism is not workable if you want to create your tab widget in Qt Designer. (Another argument in favor of implementing setExpanding as an override to the OS style rather than the way it's been done.)

like image 96
goug Avatar answered Oct 07 '22 00:10

goug