QScrollArea as central widget problem
-
As was suggested on another thread https://forum.qt.io/topic/159300/qdockwidget-oddness, I tried to add a QScrollArea to my application:
Old code:
ZTRACE_RUNTIME("Creating stackedWidget"); stackedWidget = new QStackedWidget(this); stackedWidget->setObjectName("stackedWidget"); setCentralWidget(stackedWidget); ZTRACE_RUNTIME("Creating Stacking Panel"); stackingDlg = new DSS::StackingDlg(this, pictureList); stackingDlg->setObjectName("stackingDlg"); ZTRACE_RUNTIME("Adding Stacking Panel to stackedWidget"); stackedWidget->addWidget(stackingDlg); ZTRACE_RUNTIME("Creating Processing Panel"); processingDlg = new DSS::ProcessingDlg(this); processingDlg->setObjectName("processingDlg"); ZTRACE_RUNTIME("Adding Processing Panel to stackedWidget"); stackedWidget->addWidget(processingDlg); stackedWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
new code:
ZTRACE_RUNTIME("Creating scrollarea"); scrollArea = new QScrollArea(this); scrollArea->setObjectName("scrollArea"); scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setCentralWidget(scrollArea); ZTRACE_RUNTIME("Creating stackedWidget"); stackedWidget = new QStackedWidget(this); stackedWidget->setObjectName("stackedWidget"); scrollArea->setWidget(stackedWidget); scrollArea->ensureWidgetVisible(stackedWidget); ZTRACE_RUNTIME("Creating Stacking Panel"); stackingDlg = new DSS::StackingDlg(this, pictureList); stackingDlg->setObjectName("stackingDlg"); ZTRACE_RUNTIME("Adding Stacking Panel to stackedWidget"); stackedWidget->addWidget(stackingDlg); ZTRACE_RUNTIME("Creating Processing Panel"); processingDlg = new DSS::ProcessingDlg(this); processingDlg->setObjectName("processingDlg"); ZTRACE_RUNTIME("Adding Processing Panel to stackedWidget"); stackedWidget->addWidget(processingDlg); stackedWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
The problem is that nothing is visible in the central widget area? I expected the stacked widget with its owned widgets to be there.
What have I missed or misunderstood?
-
The following works as I want which was to use a scroll bar when the widgets contained within the stacked widget hit their minimum size, and the user wants to resize the frame window smaller then would otherwise work.
ZTRACE_RUNTIME("Creating stackedWidget"); stackedWidget = new QStackedWidget(this); stackedWidget->setObjectName("stackedWidget"); scrollArea->setWidget(stackedWidget); scrollArea->ensureWidgetVisible(stackedWidget); scrollArea->setWidgetResizable(true); scrollArea->setGeometry(0, 0, 500, 500); scrollArea->setMinimumHeight(300);
David
-
As was suggested on another thread https://forum.qt.io/topic/159300/qdockwidget-oddness, I tried to add a QScrollArea to my application:
Old code:
ZTRACE_RUNTIME("Creating stackedWidget"); stackedWidget = new QStackedWidget(this); stackedWidget->setObjectName("stackedWidget"); setCentralWidget(stackedWidget); ZTRACE_RUNTIME("Creating Stacking Panel"); stackingDlg = new DSS::StackingDlg(this, pictureList); stackingDlg->setObjectName("stackingDlg"); ZTRACE_RUNTIME("Adding Stacking Panel to stackedWidget"); stackedWidget->addWidget(stackingDlg); ZTRACE_RUNTIME("Creating Processing Panel"); processingDlg = new DSS::ProcessingDlg(this); processingDlg->setObjectName("processingDlg"); ZTRACE_RUNTIME("Adding Processing Panel to stackedWidget"); stackedWidget->addWidget(processingDlg); stackedWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
new code:
ZTRACE_RUNTIME("Creating scrollarea"); scrollArea = new QScrollArea(this); scrollArea->setObjectName("scrollArea"); scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setCentralWidget(scrollArea); ZTRACE_RUNTIME("Creating stackedWidget"); stackedWidget = new QStackedWidget(this); stackedWidget->setObjectName("stackedWidget"); scrollArea->setWidget(stackedWidget); scrollArea->ensureWidgetVisible(stackedWidget); ZTRACE_RUNTIME("Creating Stacking Panel"); stackingDlg = new DSS::StackingDlg(this, pictureList); stackingDlg->setObjectName("stackingDlg"); ZTRACE_RUNTIME("Adding Stacking Panel to stackedWidget"); stackedWidget->addWidget(stackingDlg); ZTRACE_RUNTIME("Creating Processing Panel"); processingDlg = new DSS::ProcessingDlg(this); processingDlg->setObjectName("processingDlg"); ZTRACE_RUNTIME("Adding Processing Panel to stackedWidget"); stackedWidget->addWidget(processingDlg); stackedWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
The problem is that nothing is visible in the central widget area? I expected the stacked widget with its owned widgets to be there.
What have I missed or misunderstood?
@Perdrix said in QScrollArea as central widget problem:
I tried to add a QScrollArea to my application:
There's nothing wrong with adding a
QScrollArea
as central widget.
Works for me for any standard widget.I suspect your widget(s) is/are there, but you just don't see them because they are too small.
Try
stackedWidget->setGeometry(0, 0, 1000, 1000);
Do you see something now?
And how about this code?
#include <QApplication> #include <QMainWindow> #include <QPushButton> #include <QHBoxLayout> #include <QScrollArea> #include <QStackedWidget> #include <QDebug> int main(int argc, char *argv[]) { QApplication app(argc, argv); QMainWindow w; QWidget widget(&w); QPushButton pb("CLICK"); widget.setObjectName("widget"); widget.setStyleSheet("QWidget#widget { background-color: blue; }"); QHBoxLayout layout(&widget); widget.setLayout(&layout); layout.addWidget(&pb); QScrollArea *area = new QScrollArea(&w); QStackedWidget *stack = new QStackedWidget(area); // stack->setGeometry(0, 0, 500, 500); stack->addWidget(&widget); stack->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); area->ensureWidgetVisible(stack); area->setWidget(stack); w.setCentralWidget(area); w.show(); return app.exec(); }
If this works, there's something wrong with your
DSS::
classes. -
The setGeometry call worked to get it to display but the dimensions of the widgets within the stacked widget then didn't resize as the main window was re-sized. I need to think again how to handle the problem of one of the widgets in the stacked widget set being a bit larger than desired (so that it creates sizing anomalies on small screens).
D.
-
The setGeometry call worked to get it to display but the dimensions of the widgets within the stacked widget then didn't resize as the main window was re-sized. I need to think again how to handle the problem of one of the widgets in the stacked widget set being a bit larger than desired (so that it creates sizing anomalies on small screens).
D.
@Perdrix said in QScrollArea as central widget problem:
but the dimensions of the widgets within the stacked widget then didn't resize as the main window was re-sized
Then you are using
QScrollArea
wrong... or expect something different from it.
What's the point of scrollbars or a scrollable area in first place, if the widget would resize with the window?!QScrollArea
is for displaying large (static) content like huge images through a small(er) viewport.
Whenever you resize the window around it, the viewport (visible area) changes... not the scroll area's content widget. -
The following works as I want which was to use a scroll bar when the widgets contained within the stacked widget hit their minimum size, and the user wants to resize the frame window smaller then would otherwise work.
ZTRACE_RUNTIME("Creating stackedWidget"); stackedWidget = new QStackedWidget(this); stackedWidget->setObjectName("stackedWidget"); scrollArea->setWidget(stackedWidget); scrollArea->ensureWidgetVisible(stackedWidget); scrollArea->setWidgetResizable(true); scrollArea->setGeometry(0, 0, 500, 500); scrollArea->setMinimumHeight(300);
David
-
P Perdrix has marked this topic as solved on