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. Make scroll area fit added widgets horizontally, but have a vertical scrollbar if needed
Forum Updated to NodeBB v4.3 + New Features

Make scroll area fit added widgets horizontally, but have a vertical scrollbar if needed

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 2.9k 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.
  • F Offline
    F Offline
    Flashcap20
    wrote on last edited by
    #1

    Hi!

    I would like to have a scroll area that resizes the added widgets so they fit without a horizontal scrollbar.

    I am new to Qt, so I disabled the horizontal scrollbar of my scroll area, and assumed it would then make the changes needed to fit it's contents without adding a horizontal scrollbar. (Since it's disabled so who would not want their widgets to be fully visible in that case?)

    Of course, it seems like it's not that easy, disabling the scrollbar just made the scrollbar not show up, but my added widgets are overflowing to the right if they have a width greater than the scroll area.

    So is it possible to make my scroll area resize added widgets to fit horizontally, but work as it would by default vertically? (added vertical scrollbar if needed)

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

      Hi,

      I guess you are using a "container widget" that you set on your QScrollArea. So you would need to fix the width of that widget so it matches the one from the QScrollArea minus the vertical QScrollBar width.

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

        @SGaist said in Make scroll area fit added widgets horizontally, but have a vertical scrollbar if needed:

        ing a "container widget" that you set on your QScrollArea

        I tried this now, but it didn't work:

        this->ui->scrollArea_subclipsContent->setFixedWidth(this->ui->scrollArea_subclips->width() - this->ui->scrollArea_subclips->verticalScrollBar()->width());
        

        The structure of my window is as follows:
        55f60674-309e-4685-ba10-2314a9c9edbd-image.png

        So the scroll area is placed right into my central widget, and when I dropped it in there, a QWidget got created inside automatically. I then set this QWidget to have a vertical layout, so I can add widgets to it.

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

          I just re-read the QScrollArea documentation, and the bottom of the detailed description mentions your use case. It is recommended to setup the size policy of the layout you use to handle the child widgets of the container widget.

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

            Re: [Make scroll area fit added widgets horizontally](but have a vertical scrollbar if needed)

            Thank you, that was a great tip.

            I managed to do it by setting the following:

            I set both scrollArea_subclips and scrollArea_subclipsContent's Size Policy to Preferred for both Horizontal and Vertical.
            Then set the custom widget's (multiple of which is added to scrollArea_subclipsContent) Size Policy to Preferred for Horizontal and Maximum for Vertical.

            Also set the left and right margins of my custom widget to 10 instead of 5, just to make sure the widget is not touching the scrollbar once it appears.

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

              Great !

              Then please mark the thread as solved using the "Topic Tools" button or the three dotted menu beside the answer you deemed correct so that other forum users may know a solution has been found :-)

              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
              • F Flashcap20

                Re: [Make scroll area fit added widgets horizontally](but have a vertical scrollbar if needed)

                Thank you, that was a great tip.

                I managed to do it by setting the following:

                I set both scrollArea_subclips and scrollArea_subclipsContent's Size Policy to Preferred for both Horizontal and Vertical.
                Then set the custom widget's (multiple of which is added to scrollArea_subclipsContent) Size Policy to Preferred for Horizontal and Maximum for Vertical.

                Also set the left and right margins of my custom widget to 10 instead of 5, just to make sure the widget is not touching the scrollbar once it appears.

                D Offline
                D Offline
                Dariusz
                wrote on last edited by
                #7

                @Flashcap20 said in Make scroll area fit added widgets horizontally, but have a vertical scrollbar if needed:

                Re: [Make scroll area fit added widgets horizontally](but have a vertical scrollbar if needed)

                Thank you, that was a great tip.

                I managed to do it by setting the following:

                I set both scrollArea_subclips and scrollArea_subclipsContent's Size Policy to Preferred for both Horizontal and Vertical.
                Then set the custom widget's (multiple of which is added to scrollArea_subclipsContent) Size Policy to Preferred for Horizontal and Maximum for Vertical.

                Also set the left and right margins of my custom widget to 10 instead of 5, just to make sure the widget is not touching the scrollbar once it appears.

                Any chance you can drop a snippet or something? I'm not quite sure what scrollArea_subclips is :/ I want to do the same scroll logic as you do.
                Thanks!

                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