Unsolved How to ensure QScrollArea is always given enough space by its parent?
-
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?
-
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.
-
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.
-
Extract the width from the QSize object.
-
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.
-
When exactly are you doing this ?
-
@mythruss
You should use size hint of scrollAreaWidgetContents_2 -
-
As I wrote earlier, do that in a show event. Until shown, widgets have no effective sizes unless you fix it in the constructor.
-
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.
-
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.
-
-
- 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;
-
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.
-
@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.)