Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTabBar will not expand its tabs
Forum Updated to NodeBB v4.3 + New Features

QTabBar will not expand its tabs

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 5.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lucianmanescu
    wrote on last edited by
    #1

    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();
    ((QVBoxLayout*)(centralwidget->layout()))->insertWidget(0, m_pTabBar);

    m_pTabBar->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
    m_pTabBar->setExpanding(true);

    m_pTabBar->addTab("Login Tab");
    m_pTabBar->addTab("User profile");
    m_pTabBar->addTab("User Session");

    m_pTabBar->show();
    @

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lucianmanescu
        wrote on last edited by
        #3

        Hello,

        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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lucianmanescu
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • L Offline
              L Offline
              lucianmanescu
              wrote on last edited by
              #6

              I got back to my code after some bug fixing and I have created a small application that reproduces the issue I have:
              tabbar_expand.ui
              @<?xml version="1.0" encoding="UTF-8"?>
              <ui version="4.0">
              <class>MainWindow</class>
              <widget class="QMainWindow" name="MainWindow">
              <property name="geometry">
              <rect>
              <x>0</x>
              <y>0</y>
              <width>800</width>
              <height>600</height>
              </rect>
              </property>
              <property name="windowTitle">
              <string>MainWindow</string>
              </property>
              <widget class="QWidget" name="centralwidget">
              <layout class="QVBoxLayout" name="verticalLayout">
              <item>
              <widget class="QPushButton" name="pushButton">
              <property name="text">
              <string>PushButton</string>
              </property>
              </widget>
              </item>
              </layout>
              </widget>
              </widget>
              <resources/>
              <connections/>
              </ui>@

              mainwindow.h
              @#include <QMainWindow>
              #include "ui_tabbar_expand.h"

              class MainWindow : public QMainWindow, public Ui_MainWindow
              {
              Q_OBJECT

              public:
              MainWindow(QWidget *parent = 0, Qt::WindowFlags flags = 0);
              virtual ~MainWindow(){};
              };@

              mainwindow.cpp
              @#include "mainwindow.h"

              MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags)
              :QMainWindow(parent, flags)
              {
              setupUi(this);

              QTabBar* pTabBar = new QTabBar();
              qobject_cast<QVBoxLayout*>(centralwidget->layout())->insertWidget(0, pTabBar);

              //pTabBar->addTab("Login Tab");
              //pTabBar->addTab("User profile");
              //pTabBar->addTab("User Session");

              pTabBar->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
              pTabBar->setExpanding(true);

              pTabBar->addTab("Login Tab");
              pTabBar->addTab("User profile");
              pTabBar->addTab("User Session");

              pTabBar->show();
              }@

              main.cpp
              @#include <qapplication.h>
              #include "mainwindow.h"

              int main(int argc, char *argv[])
              {
              QApplication app(argc, argv);

              MainWindow w;
              w.show();

              return app.exec();
              }@

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved