Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Making Entire Dialog Scrollable
RayGunYoda last edited by
I have existing dialog designs that were created using the qt designer. These dialogs are meant to be shown at their maximum size, but they are re-sizable (can only make smaller). When they are dragged to be smaller, my goal is to make horizontal and vertical scroll bars appear to scroll through the entire dialog. These scroll bars should mimic scrollbars for most major applications (eg. after searching for something in chrome, horizontal and vertical scroll bars appear when resizing the window).
From what I have read, people suggest using QScrollArea with a QLayout, but I am not sure how to make this work for an existing dialog design (without a main layout). Maybe I just don't understand how to use layouts correctly, but if I right click the dialog in qt designer and click on any layout, everything moves and completely changes the location and design of the dialog. What is the proper way to add the scrolling functionality as described without having to redesign the dialog to use embedded layouts?
Hi and welcome to devnet,
It's not QScrollArea + layout. You set a widget as the QScrollArea content and then put the rest in a layout that you apply to that widget.
JonB last edited by
without having to redesign the dialog to use embedded layouts
As @SGaist has said. But your dialog should already have a layout, are you saying yours does not have one? There may be a better way for dialogs, but for your scrolling I think you're supposed to have:
QDialog -> layout -> QScrollArea -> QWidget -> layout -> all the widgets you have on it now.
RayGunYoda last edited by
@JonB the dialog does not have a layout. Since it sounds like that having a layout is a requirement for scrolling, is there a way to insert all the widgets into a layout such that their positions stay the same as they are now?
I found this code in another thread online:
QScrollArea *scroll = new QScrollArea(this); QWidget *viewport = new QWidget(this); scroll->setWidget(viewport); QHBoxLayout *l = new QHBoxLayout(viewport); viewport->setLayout(l); // add all dialog widgets to layout l // ... adding widgets ... // Add a layout for QDialog QHBoxLayout *dialog_layout = new QHBoxLayout(dialog); this->setLayout(dialog_layout); this->layout()->addWidget(scroll);
I put this code in the constructor for my dialog. Firstly, I am not sure how to add all of the widgets to the layout. I tried this:
QList<QWidget*> list = this->findChildren<QWidget*>(); for (QWidget * w : list) l->addWidget(w);
But this crashes. I also tried manually listing each widget (ie l->addWidget(ui->widgetName)), but this shows just a blank rectangle (also does not show scroll bars when the dialog is dragged to be smaller).
Do you have any suggestions for what to do to get the scrollbar functionality working while maintaining the positioning of the widgets I already have?