PDA

View Full Version : Qt C++ ScrollArea



SMF
2nd January 2019, 17:47
I have begun using Qt to construct a GUI for a C++ program that I have written. I have widgets that go down past where the screen of my computer goes and want to be able to scroll down to these widgets when I run my program. I have placed all of my objects in a scroll area and selected both vertical and horizontal scroll bar as “always on” but still can’t scroll down to my widgets. Any help is much appreciated.

d_stranz
2nd January 2019, 23:10
Any help is much appreciated.

How did you add these widgets to the scroll area? Did you create a composite widget (a QWidget with a layout and children) and did you add that QWidget using QScrollArea::setWidget()?

The Qt Image Viewer example (https://doc.qt.io/qt-5/qtwidgets-widgets-imageviewer-example.html) shows how to properly use a QScrollArea with scrolling and resizing to fit an image.

ChrisW67
2nd January 2019, 23:29
QScrollArea provides a scrolling view over a single widget. You will not be able to (need to) scroll in either direction unless the size of that single widget is larger than the available screen space occupied by the QScrollArea.

You will need to demonstrate how you "placed all of my objects in a scroll area" and how you have placed the scroll area on screen.

Edit: distracted for a few minutes and beaten to the punch by d_stranz.

SMF
3rd January 2019, 02:01
I simply dragged the scroll area onto the main window anddragged my objects onto the scroll area in the Qt designer - no code.

d_stranz
3rd January 2019, 16:13
I simply dragged the scroll area onto the main window and dragged my objects onto the scroll area in the Qt designer - no code.

Well that probably created an incorrect configuration for the scroll area. If you open the .ui file as text (not a GUI view), you will see the XML that the designer writes when creating the UI. If the scroll area was done correctly, you should see a hierarchy that starts with your QMainWindow, inside that should be the QScrollArea (and the centralWidget), inside that should be a single QWidget (the one the scroll area manages), inside that should be some kind of layout, and finally inside that should be your widgets that the layout will manage.

If all you did was add the scroll area to the main window, you'll probably see the first two levels, but then inside the scroll area will be multiple widgets at the same level. That isn't something the QScrollArea can handle correctly.

I would suggest that you do the following:

In Qt Creator:

- derive a new GUI class from QWidget, call it whatever you want that makes sense. This will be the widget managed by the scroll area.

In Qt Designer:

- add an appropriate layout to that new widget using the designer
- add the child widgets for your UI inside the layout
- save it all
- you will end up with .ui, .h, and .cpp files for this new widget

Back in Qt Designer:

- in your main window code, add the scroll area as the central widget
- add a plain old QWidget to the scroll widget
- promote that QWidget (https://doc.qt.io/qt-5/designer-using-custom-widgets.html) to whatever you have named your new widget.
- save it all and build

If you have the scroll area set to use scrolling automatically, then as you reduce the size of the main window to smaller than your new widget, the scrollbars should appear and you can scroll the content.

SMF
3rd January 2019, 17:46
Thank you so much for your help. I'll try this out and see how it goes.