Make scroll area fit added widgets horizontally, but have a vertical scrollbar if needed
-
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)
-
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.
-
@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:
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.
-
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.
-
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.
-
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 :-)
-
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.
@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!