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. [QTabWidget] Setting client width?
Forum Updated to NodeBB v4.3 + New Features

[QTabWidget] Setting client width?

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 4 Posters 5.8k 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.
  • J Offline
    J Offline
    J0J0
    wrote on last edited by
    #1

    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());
    

    QtCreator: 3.5.1
    Qt: 5.5.1
    OS: x86_64-linux-gnu, Ubuntu 16.04.2 LTS "xenial"
    Compiler: GCC v5.2.1

    VRoninV 1 Reply Last reply
    0
    • J J0J0

      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());
      
      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      @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());
      ui->tabWidget->resize(tabSize);
      

      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);

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      J 1 Reply Last reply
      2
      • VRoninV VRonin

        @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());
        ui->tabWidget->resize(tabSize);
        

        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);

        J Offline
        J Offline
        J0J0
        wrote on last edited by
        #3

        @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?

        QtCreator: 3.5.1
        Qt: 5.5.1
        OS: x86_64-linux-gnu, Ubuntu 16.04.2 LTS "xenial"
        Compiler: GCC v5.2.1

        VRoninV 1 Reply Last reply
        0
        • J J0J0

          @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?

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          @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.

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          J 1 Reply Last reply
          2
          • VRoninV VRonin

            @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.

            J Offline
            J Offline
            J0J0
            wrote on last edited by
            #5

            @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).

            QtCreator: 3.5.1
            Qt: 5.5.1
            OS: x86_64-linux-gnu, Ubuntu 16.04.2 LTS "xenial"
            Compiler: GCC v5.2.1

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

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

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              J 1 Reply Last reply
              1
              • VRoninV VRonin

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

                J Offline
                J Offline
                J0J0
                wrote on last edited by
                #7

                @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??

                QtCreator: 3.5.1
                Qt: 5.5.1
                OS: x86_64-linux-gnu, Ubuntu 16.04.2 LTS "xenial"
                Compiler: GCC v5.2.1

                1 Reply Last reply
                0
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Hi
                  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.

                  J 1 Reply Last reply
                  2
                  • mrjjM mrjj

                    Hi
                    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.

                    J Offline
                    J Offline
                    J0J0
                    wrote on last edited by
                    #9

                    @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));
                            ui->tabWidget->updateGeometry();
                            ui->tabLayout->layout();
                    

                    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...

                    QtCreator: 3.5.1
                    Qt: 5.5.1
                    OS: x86_64-linux-gnu, Ubuntu 16.04.2 LTS "xenial"
                    Compiler: GCC v5.2.1

                    1 Reply Last reply
                    0
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by mrjj
                      #10

                      @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.

                      https://www.youtube.com/watch?v=gWa2rqe8l6E

                      JonBJ J 2 Replies Last reply
                      0
                      • mrjjM mrjj

                        @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.

                        https://www.youtube.com/watch?v=gWa2rqe8l6E

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #11

                        @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?

                        1 Reply Last reply
                        0
                        • mrjjM mrjj

                          @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.

                          https://www.youtube.com/watch?v=gWa2rqe8l6E

                          J Offline
                          J Offline
                          J0J0
                          wrote on last edited by
                          #12

                          @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.

                          QtCreator: 3.5.1
                          Qt: 5.5.1
                          OS: x86_64-linux-gnu, Ubuntu 16.04.2 LTS "xenial"
                          Compiler: GCC v5.2.1

                          1 Reply Last reply
                          0
                          • VRoninV Offline
                            VRoninV Offline
                            VRonin
                            wrote on last edited by VRonin
                            #13

                            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{
                            Q_DISABLE_COPY(BigStackedWidget)
                            public:
                            BigStackedWidget(QWidget* parent = Q_NULLPTR)
                            :QStackedWidget(parent){}
                            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);
                            }
                            };
                            

                            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                            ~Napoleon Bonaparte

                            On a crusade to banish setIndexWidget() from the holy land of Qt

                            1 Reply Last reply
                            1

                            • Login

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