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. How to ensure QScrollArea is always given enough space by its parent?

How to ensure QScrollArea is always given enough space by its parent?

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 4 Posters 2.0k Views 3 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.
  • M Offline
    M Offline
    mythruss
    wrote on last edited by mythruss
    #1

    As shown in my screenshot, the QScrollArea isn't given enough width by the QTabWidget to display all of the widgets. I'm using a ScrollArea because I want the option of being able to scroll vertically but I always want enough width to display all of the scroll area widgets. I know I can set a minimum size for the TabWidget that extends the width far enough and looks good on my monitor but that isn't consistent on different machines/monitors. Is there any way to ensure the TabWidget always gives the ScrollArea adequate width?

    Screenshot (25).png

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

      Hi,

      One way would be to set the minimum width of your QTabWidget in the showEvent by requesting the sizeHint of the widget contained by the QScrollArea.

      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
      • M Offline
        M Offline
        mythruss
        wrote on last edited by
        #3

        How would I go about implementing that? I tried:

        ui->tabWidget->setMinimumWidth(ui->scrollArea_2->sizeHint());

        but ofcourse it can't convert type QSize to an int.

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

          Extract the width from the QSize object.

          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
          • M Offline
            M Offline
            mythruss
            wrote on last edited by
            #5

            So I tried that but the problem is all of the sizehints being returned are too small to display all the widgets, hence why the tabwidget doesn't give enough room.

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

              When exactly are you doing this ?

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

              M 1 Reply Last reply
              0
              • M mythruss

                So I tried that but the problem is all of the sizehints being returned are too small to display all the widgets, hence why the tabwidget doesn't give enough room.

                B Offline
                B Offline
                Bonnie
                wrote on last edited by Bonnie
                #7

                @mythruss
                You should use size hint of scrollAreaWidgetContents_2

                1 Reply Last reply
                0
                • SGaistS SGaist

                  When exactly are you doing this ?

                  M Offline
                  M Offline
                  mythruss
                  wrote on last edited by
                  #8

                  @SGaist
                  I tried resizing in the mainwindow constructor.

                  @Bonnie
                  That is the size hint I tried using, but for some reason that size hint is too small to contain the widgets.

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

                    As I wrote earlier, do that in a show event. Until shown, widgets have no effective sizes unless you fix it in the constructor.

                    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
                    • M Offline
                      M Offline
                      mythruss
                      wrote on last edited by
                      #10

                      Excuse me if I sound like a noob but can you direct me to where I find/make a show event? The sizehint that gets returned in the constructor is the same size the tabwidget ends up being without trying to set any minimum widths, so it definitely has a size at that point.

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mythruss
                        wrote on last edited by
                        #11

                        I guess a more important question is why aren't the sizeHints correct in the first place? You would think that the TabWidget would automatically try to fit all of its children widgets inside of it instead of cutting them off.

                        B 1 Reply Last reply
                        0
                        • M mythruss

                          I guess a more important question is why aren't the sizeHints correct in the first place? You would think that the TabWidget would automatically try to fit all of its children widgets inside of it instead of cutting them off.

                          B Offline
                          B Offline
                          Bonnie
                          wrote on last edited by
                          #12

                          @mythruss
                          That's a thing with QScrollArea.
                          It save it's content widget's size hint when the first time sizeHint() is called and never change that size.
                          But, as @SGaist said, the size hint before showEvent is not effective , especially with any text QLabel inside the layout.

                          1 Reply Last reply
                          0
                          • mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13
                            • Excuse me if I sound like a noob but can you direct me to where I find/make a show event?

                            Hi, just in case you still wonder.
                            showEvent is a virtual function so you subclass to override it.

                            protected:
                                virtual void showEvent(QShowEvent *event) override;
                            
                            1 Reply Last reply
                            1
                            • M Offline
                              M Offline
                              mythruss
                              wrote on last edited by
                              #14

                              Thank you everyone for the help. I did as you all suggested and resized the minimum width of the tabWidget to be the sizeHint width of the scrollArea. Looks like it now covers up less of the scrollArea widgets but still isn't perfect. I think now I just need to account for the width of the border of the tab widget itself and for when the vertical scroll bar is visible.

                              B 1 Reply Last reply
                              0
                              • M mythruss

                                Thank you everyone for the help. I did as you all suggested and resized the minimum width of the tabWidget to be the sizeHint width of the scrollArea. Looks like it now covers up less of the scrollArea widgets but still isn't perfect. I think now I just need to account for the width of the border of the tab widget itself and for when the vertical scroll bar is visible.

                                B Offline
                                B Offline
                                Bonnie
                                wrote on last edited by
                                #15

                                @mythruss
                                I've written a subclass of QScrollArea in this post:
                                https://forum.qt.io/topic/113940/resizing-qscrollarea-issue
                                I think maybe you can try to promote scrollArea_2 to this class, and then set the horizontal size policy of scrollArea_2 to MinimumExpanding. (No need to set minimum width in this case.)

                                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