Important: Please read the Qt Code of Conduct -

[QTabWidget] Setting client width?

  • Hi, during construction of the main window, I am adding widgets to a tab widget. How can I set the client height of the tab widget so that the tab widget can display all the sheets? Something like

    tabWidget->addTab (sheet, "Caption");
    tabWidget->ClientHeight = std::max (tabWidget->ClientHeight, sheet->Height);

    For example, the following line doesn't even change the tabsheet's height (lest alone it would change the height of the complete tab widget and not its client height)

    ui->tabWidget->size().rheight() = std::max (ui->tabWidget->height(), ui->sheet->height());

  • @J0J0 said in [QTabWidget] Setting client width?:

    ui->tabWidget->size().rheight() =

    Coming to C++ from a language with properties I guess. ui->tabWidget->size() returns a copy of the size not a reference to it. you have to use the get->change->set paradigm

    QSize tabSize = ui->tabWidget->size();
    tabSize.rheight() =std::max (tabSize->height(), ui->sheet->height());

    This of course will not work if tabWidget is in a layout as it will take over resizing it. In that case replace ui->tabWidget->resize(tabSize); with ui->tabWidget->setMinimumSize(tabSize);

  • @VRonin

    Hi, thanks for your answer. However I don't understand what you mean by "replace".

    Layout managers are not a problem at the moment, the settings are accepted and displayed. My problem is that I have no idea how to determine the client width. Suppose

        void addSheet (QTabWidget *tabs, QGroupBox *sheet)
            tabs->addTab (sheet, sheet->title());
            tabs->setGeometry (sheet->geometry());
            sheet->setTitle ("");

    and adding just one sheet. After adding the sheet, the tab widget is too small to display the client (the group box) because the tab widget also displays the tabs, maybe some border and margin etc, hence the client is clipped because the tab widget mist be /larger/ than the client.

    What I am looking for is how to determine the additional space that's needed so that the client displays properly and is not clipped. For example, some GUI builders allow setting client width and height directly so that the parent resizes properly. What's the according replacement in Qt?

  • @J0J0 said in [QTabWidget] Setting client width?:

    Layout managers are not a problem at the moment

    They are THE problem here.

    don't use tabs->setGeometry(sheet->geometry()); but, instead, just add the QTabWidget you pass to this function to a layout.

  • @VRonin

    Sorry, I don't get it. I added the tabswidget to a layout and then when adding the sheets:

        void addSheet (QTabWidget *tabs, QGroupBox *sheet)
            tabs->addTab (sheet, sheet->title());
            QGridLayout *lay = new QGridLayout();
            QSize size = tabs->layout() ? tabs->layout()->minimumSize() : QSize();
            int maxHeight = std::max (sheet->height(), size.height());
            int maxWidth  = std::max (sheet->width(), size.width());
            lay->setGeometry (QRect(QPoint(0,0), QSize(maxWidth, maxHeight)));
            tabs->setLayout ((QLayout*)lay);
            sheet->setTitle ("");

    --Even though I added a layout for tabswidget, in the first call to above function layout() is null. Moreover, I cannot change the layout but have to pop a new layout each time?

    -- layout does not allow to set minimum size (which would be the maximum size over all client sheets plus the extra space needed by the tabs widget).

  • No, you should not do the layout in that function. The layout must be done outside, when you create the QTabWidget

  • @VRonin
    Still doesn't work...

            QGridLayout *lay = new QGridLayout();
            s = QSize (100, 100);
            lay->setGeometry (QRect(QPoint(0,0), s));
            ui->tabWidget->setLayout (/*(QLayout*)*/lay);

    doesn't adjust the tabWidget's size at all.

    Must I add the sheets also to the layout? I used grid layout because I don't know a reasonable layout; the grid would just have 1 x 1 iteme (the sheed displayed when the respective tab is visible).

    Can this be so complicated??

  • Lifetime Qt Champion

    The parent widget that hold the the tabWidget should have a
    layout so it will make the tabwidget use all of the parents space.
    If a mainwindow, you would normally add a layout to centralwidget
    Also inside the actual tabs should be a layout so the widgets in it, will use all of the tab space.

    Since you have UI->, just go to designer, right click the parent widget where
    you put the tabwidget in and use the layout menu.

  • @mrjj

    still no effect:

    In gui, I added a Layout named "tabLayout" and dragged the tabWidget into it. Size constraint is "SetMinimumSize".

    Then in mainwindow I try to set the client size, curently hard coded to "200 x 200":

            s = QSize (200, 200);
            int left = ui->tabWidget->geometry().left();
            int top = ui->tabWidget->geometry().top();
            ui->tabLayout->setGeometry (QRect (QPoint (left, top), s));

    How can I "apply" that size? As there is nothing like "setClientSize", is "setGeometry" on the layout the right thing to do? I was already told that setting the geometry of the tab widget itself is a bad thing to do...

  • Lifetime Qt Champion

    @J0J0 said in [QTabWidget] Setting client width?:

    I added a Layout named "tabLayout"

    By using right click ?
    not the one from the left side with the widgets (red color), its not for that.

    you dont apply any size. when done right. it will follow parent. ( mainwindow)

    you could watch some yourtube videos about layouts.
    you seems to expect something that it not true. you dont use
    setGeometry with layouts as it dont work that way at all.

  • @mrjj
    Yes, I have been following this thread and just wanted to check my own understanding with you, please?

    I have inherited large code which uses QTabWidgets. So far as I am aware, they are plonked on a layout and then filled with whatever controls, and they take up the room of whatever their content (rather than any size on their container, though it might be other way round) is, I think. Does that sound about right?

  • @mrjj said in [QTabWidget] Setting client width?:

    @J0J0 said in [QTabWidget] Setting client width?:

    I added a Layout named "tabLayout"

    By using right click ?
    not the one from the left side with the widgets (red color), its not for that.

    Hä? Now I am totally confused... Shouldn't I add some QLayout for that purpose?

    you dont apply any size. when done right. it will follow parent. ( mainwindow)

    As explained in post #1, what I want is that the tabwidged adjusts to the size of the clients, i.e. the size of the maximum client.

    I can use tabWidget->setMinimumSize (size);

    But I have ho Idea how to compute that size. All I know is the maximum size over all clients, or children or whatever naming is used for such entities.

    And AFAIU Qt has nothing like QWidget->setClientSize (...).

    As explained in post #1, the tab widged consumes extra space for displaying the tabs (the entities that display the labels and icons) and extra space for border, frame etc. Hence the tab widget's size is client_size + label_size + border_size.

    What I am seeking is either a was to set the client size, or to compute the difference between client size and the size of the hosting widget.

  • what I want is that the tabwidged adjusts to the size of the clients, i.e. the size of the maximum client.

    QTabWidget is just a QStackedWidget + QTabBar. You can subclass QStackedWidget and reimplement sizeHint to look at all widgets not just the current one and then manually link the QTabBar to QStackedWidget (it's 1 connect statement)

    class BigStackedWidget : public QStackedWidget{
    BigStackedWidget(QWidget* parent = Q_NULLPTR)
    virtual QSize sizeHint() const Q_DECL_OVERRIDE{
    int maxW=-1;
    int maxH=-1;
    for(int i=0;i<count();++i){
    const QSize tempSize = widget(i)->sizeHint();
    maxW = qMax(maxW,tempSize.width());
    maxH = qMax(maxH,tempSize.height());
    return QSize(maxW,maxH);

Log in to reply