Widgets do not expand to fill the entire vertical space of the screen ?
-
Hi, I have a
QTableView
,QListView
, andQScrollArea
. The layouts are shown below.For the
QTableView
it automatically grows vertically to the height of the available screen before scroll bars are shown.For
QListView
andQScrollArea
however, they do not grow to the full vertical height of the screen before scroll bars are shown. I have tried setting the vertical size policy of almost all the parent layouts/widgets toExpanding
, but it seems to make no difference.I don't understand why it's working for
QTableView
but not the others. I can't seem to find any pattern in the parent widgets that causes this? -
Hi,
Not knowing how you coded that prevents us from giving you good answer.
Some observations:
- putting a vertical layout in a form layout is not best idea. QFormLayout is designed to build forms
- Stacking so many QFrame seems odd from a GUI perspective.
- I would recommend building each complex tab of your tab widget as an independent widget. You will avoid creating a complex enormous single monster widget that will be a pain to maintain.
-
The issue is that you seem to try to cobble stuff together and "let it work".
I would recommend starting with just a pen and paper to visualise the UI you want and then start designer. Learn about Qt's layout classes and then build small thing one after the other using the minimal amount of building blocks.
Personally I build UIs only in code because I am faster doing so and have a clearer picture of what goes where.
-
The QScrollArea, your outermost widget, provides a view onto an internal widget of indeterminate size. The internal widget may be bigger, smaller, or the same size as the viewport the scroll area has to display it in. The QScrollArea does not resize the internal widget in any way, just displays some or all of it.
The QTabWidget (blue one) you use as the scrollable widget will have either a "natural" size based on the constraints of the widgets it contains, or some sort of fixed size that you control. It is possible to wait until the scroll area is displayed, determine the size of the viewport, and resize the QTabWidget to match that. After that the tab widget will remain fixed in size and the scroll area will not have to produce scroll bars unless the QScrollArea widget is resized down.
In the case of the table view, where the drawn table has more content than will fit on the screen (i.e. this widget is bigger than the viewport), the scroll area will display more of the underlying table as the view port grows until the screen size constrains the QScrollArea. Make your QTabWidget widget taller than the available screen height and you will see the same behaviour.
-
@ChrisW67 said in Widgets do not expand to fill the entire vertical space of the screen ?:
The QScrollArea, your outermost widget, provides a view onto an internal widget of indeterminate size. The internal widget may be bigger, smaller, or the same size as the viewport the scroll area has to display it in. The QScrollArea does not resize the internal widget in any way, just displays some or all of it.
The QTabWidget (blue one) you use as the scrollable widget will have either a "natural" size based on the constraints of the widgets it contains, or some sort of fixed size that you control. It is possible to wait until the scroll area is displayed, determine the size of the viewport, and resize the QTabWidget to match that. After that the tab widget will remain fixed in size and the scroll area will not have to produce scroll bars unless the QScrollArea widget is resized down.
In the case of the table view, where the drawn table has more content than will fit on the screen (i.e. this widget is bigger than the viewport), the scroll area will display more of the underlying table as the view port grows until the screen size constrains the QScrollArea. Make your QTabWidget widget taller than the available screen height and you will see the same behaviour.
Thank-you for the explanation. In the case of the 2nd diagram, the viewport is is obviously not taking up the full height of the screen then. The outer
scrollArea
has no scrollbars and is taking up half the screen height. The innerscrollArea
has more content than what can be displayed thus creating inner scrollbars. I still am unsure as to how I can get the outerscrollArea
to have a larger viewport up to the vertical height of the screen. -
@R-P-H The outermost QScrollArea size is driven by one of two things:
- The layout that it is placed in if it is not a top-level widget.
- The size you set if it is a top-level widget.
I suspect you are in the second group. The top-level widget will adopt a default size based on its content and any constraints place on it by the window manager... it does not have to stay that way.
It seems you want to call scrollArea->resize() to set the height to the screen height. You can get the screen dimensions something like:QScreen *screen = app->primaryScreen(); scrollArea->resize(screen.availableGeometry().size()); // or after Qt 5.14 QScreen *screen = scrollArea->screen(); scrollArea->resize(screen.availableGeometry().size());
You may need to arrange this to happen after the widget is displayed by triggering it with a zero duration, single shot QTimer.
-
@ChrisW67 Thanks ! The resizing works but
screen.availableGeometry().size()
is giving the incorrect size (much too big). I tried a variety of other ways but can't seem to get the correct available size even when running it after the widget is displayed using a timer as suggested.