Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QTabBar will not expand its tabs
I have a QTabBar which is created and added to a vertical layout at runtime.
It will not resize its tabs even if the setExpanding is set to true.
m_pTabBar = new QTabBar();
I want to expand the three tabs to cover the entire width of the main window.
Anyone having any clue why it does not expand?
Hi and welcome to devnet,
Do you mean having each tab taking 1/3rd of the width ? If so you'll have to do it yourself, QTabBar follows the platform style which generally doesn't make tabs look like that.
On a related note, shouldn't you be using QTabWidget ?
One last thing:
@ ((QVBoxLayout*)(centralwidget->layout()))->insertWidget(0, m_pTabBar);@
This is a bad idea, don't use C style cast when dealing with QObject derived class. Use qobject_cast
Yes, having 1/3.333333333 for each tab so the entire width of the tab bar is filled with tabs; that is my goal.
C style cast is not the problem. I know it is not the way to go but it was quicker to write the code as an example.
The issue here is the QTabBar which is not resizing the tabs. Why is the "expanding" property there if it does not work? I quote the documentation : "This property holds when expanding is true QTabBar will expand the tabs to use the empty space." It does not do such thing.
The QTabWidget cannot be used as I have to use QTabBar in a combination with a QStackedWidget.
Well, in fact it is but it's respecting the OS Style. If you set the property to false you'll see that the tabs will stay big enough to contain the text but will not grow if you make it larger.
I will have to get in the office on Monday to make some screen shots as I found it very interesting and spent quite some time on it.
I got back to my code after some bug fixing and I have created a small application that reproduces the issue I have:
@<?xml version="1.0" encoding="UTF-8"?>
<widget class="QMainWindow" name="MainWindow">
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout">
<widget class="QPushButton" name="pushButton">
class MainWindow : public QMainWindow, public Ui_MainWindow
MainWindow(QWidget *parent = 0, Qt::WindowFlags flags = 0);
MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags)
QTabBar* pTabBar = new QTabBar();
int main(int argc, char *argv)
QApplication app(argc, argv);
Again, QTabBar is respecting your OS style so it's not a bug or a misbehavior.
You'll have to subclass QTabBar and reimplement tabSizeHint to achieve what you want.