Expand QScrollArea after child widget reloading
-
Hey guys,
I have a tab widget (QTabWidget), which consists of pages (subclassed from QWidget).
One of the tab pages acts as a navigation page, by having buttons: next and back to view the content of the dedicated page. This tab page has a method setCurrentPageIndex() so that I could inform it which page to load for viewing. When the tab widget sends a signal currentChanged(), I call setCurrentPageIndex(0) on the tab page to instruct the load the first page.This tab page when constructed only internally creates QScrollArea and rest of the setup is done when setCurrentPageIndex() is triggered.
Here is code extract from setCurrentPageIndex(). All the widgets and layouts are recreated from scratch and I delete previously setup child widget in QScrollArea.
auto pageLayout = new QVBoxLayout(); // Populate it with wigets auto pageWidget = new QWidget(); pageWidget->setLayout(pageLayout); auto oldPageWidget = mScrollArea->takeWidget(); if (oldPageWidget != nullptr) oldPageWidget->deleteLater(); mScrollArea->setWidget(pageWidget); mScrollArea->setWidgetResizable(true);Despite applying mScrollArea->setWidgetResizable(true), the scroll area does not expand to the whole size of the tab page. I did try pageWidget->resize() but did not get any changes.

Also, when I was working initially on this tab page without navigation logic yet and the widgets were setup only once when the page is created (not each time setCurrentPageIndex() is called) the QScrollArea would expand to the whole tab page size.
Anything odd you notice here? Could it be update/refresh issue?
Thanks!
-
Hey guys,
I have a tab widget (QTabWidget), which consists of pages (subclassed from QWidget).
One of the tab pages acts as a navigation page, by having buttons: next and back to view the content of the dedicated page. This tab page has a method setCurrentPageIndex() so that I could inform it which page to load for viewing. When the tab widget sends a signal currentChanged(), I call setCurrentPageIndex(0) on the tab page to instruct the load the first page.This tab page when constructed only internally creates QScrollArea and rest of the setup is done when setCurrentPageIndex() is triggered.
Here is code extract from setCurrentPageIndex(). All the widgets and layouts are recreated from scratch and I delete previously setup child widget in QScrollArea.
auto pageLayout = new QVBoxLayout(); // Populate it with wigets auto pageWidget = new QWidget(); pageWidget->setLayout(pageLayout); auto oldPageWidget = mScrollArea->takeWidget(); if (oldPageWidget != nullptr) oldPageWidget->deleteLater(); mScrollArea->setWidget(pageWidget); mScrollArea->setWidgetResizable(true);Despite applying mScrollArea->setWidgetResizable(true), the scroll area does not expand to the whole size of the tab page. I did try pageWidget->resize() but did not get any changes.

Also, when I was working initially on this tab page without navigation logic yet and the widgets were setup only once when the page is created (not each time setCurrentPageIndex() is called) the QScrollArea would expand to the whole tab page size.
Anything odd you notice here? Could it be update/refresh issue?
Thanks!
-
Hey @jsulm! I am not exactly sure I follow what you refer to by "not in the layout". From what I understand in the documentation, QScrollArea is not a part of the layout explicitly. The QScrollArea expects a child widget to be set (setWidget) and the child widget should have layout already setup on it. This is how I am creating QScrollArea
TestPage::TestPage(Test* test, QWidget* parent) : QWidget(parent) , mTest(test) { setupWidgets(); } void TestPage::setupWidgets() { // Create Previous, Next buttons auto navigationLayout = new QHBoxLayout(); // Add Previous, Next buttons mPageLayout = new QVBoxLayout(); mPageLayout->addLayout(navigationLayout); auto pageWidget = new QWidget(); pageWidget->setLayout(mPageLayout); mScrollArea = new QScrollArea(this); mScrollArea->setWidgetResizable(true); mScrollArea->setWidget(pageWidget); setCurrentPageIndex(0); }Thanks!
-
Hey @jsulm! I am not exactly sure I follow what you refer to by "not in the layout". From what I understand in the documentation, QScrollArea is not a part of the layout explicitly. The QScrollArea expects a child widget to be set (setWidget) and the child widget should have layout already setup on it. This is how I am creating QScrollArea
TestPage::TestPage(Test* test, QWidget* parent) : QWidget(parent) , mTest(test) { setupWidgets(); } void TestPage::setupWidgets() { // Create Previous, Next buttons auto navigationLayout = new QHBoxLayout(); // Add Previous, Next buttons mPageLayout = new QVBoxLayout(); mPageLayout->addLayout(navigationLayout); auto pageWidget = new QWidget(); pageWidget->setLayout(mPageLayout); mScrollArea = new QScrollArea(this); mScrollArea->setWidgetResizable(true); mScrollArea->setWidget(pageWidget); setCurrentPageIndex(0); }Thanks!
@Kolya said in Expand QScrollArea after child widget reloading:
I am not exactly sure I follow what you refer to by "not in the layout"
What I mean is: if the scroll area is not managed by a layout (or its size by you manually) it will not be resized if you resize the widget where this scroll area is located.