Shrinking for QDialog with QStackedWidget in it is not working
-
This is my simple dialog without
QStackedWidget
:
MyDialog::MyDialog(QWidget *parent) : QDialog(parent), ui(new Ui::MyDialog) { ui->setupUi(this); ShowFrame(false); connect(ui->checkBox, &QCheckBox::toggled, this, [this](bool checked) { ShowFrame(checked); }); } void MyDialog::ShowFrame(bool isHidden) { ui->frame->setVisible(isHidden); adjustSize(); }
And it works fine. Whenever I press the checkbox the dialog grows/shrinks.
But after I add
QStackedWidget
inQDialog
, shrinking is not working anymore.- Starts like this:
- Then grows after pressing the checkbox:
- And never shrinks back:
Here's the updated dialog with
QStackedWidget
:
MyDialogWithStackedWidget::MyDialogWithStackedWidget(QWidget *parent) : QDialog(parent), ui(new Ui::MyDialogWithStackedWidget) { ui->setupUi(this); ShowFrame(false); connect(ui->checkBox, &QCheckBox::toggled, this, [this](bool checked) { ShowFrame(checked); }); } void MyDialogWithStackedWidget::ShowFrame(bool isShown) { ui->frame->setVisible(isShown); ui->stackedWidget->adjustSize(); adjustSize(); }
What am I missing?
- Starts like this:
-
@Kelly-Ryan said in Shrinking for QDialog with QStackedWidget in it is not working:
void MyDialog::ShowFrame(bool isHidden)
{
ui->frame->setVisible(isHidden);
adjustSize();
}https://stackoverflow.com/questions/14480696/resize-qstackedwidget-to-the-page-which-is-opened
-
I made
ResizableStackedWidget
:ResizableStackedWidget::ResizableStackedWidget(QWidget *parent) : QStackedWidget(parent) { connect(this, &ResizableStackedWidget::currentChanged, this, [this](int index) { QWidget* pWidget = widget(index); Q_ASSERT(pWidget); pWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); pWidget->adjustSize(); adjustSize(); }); } int ResizableStackedWidget::addWidget(QWidget* pWidget) { pWidget->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); return QStackedWidget::addWidget(pWidget); }
And promoted my old QStackedWidget to this new one.
On dialog creation
ResizableStackedWidget::addWidget
is called withpage
andpage_2
widgets.
AlsocurrentChanged
is triggered withindex = 0
.
So at the end we have:
page
= Expanding, Expanding
page_2
= Ignored, Ignored
It doesn't help me. MyQDialog
is still not shrinking after the frame is hidden.I've read about the resizable stacked widget and it doesn't seem to be what I want. It's resized whenever you change the pages of
QStackedWidget
.
My issue is that after some widget is hidden the whole dialog is not resized to fit minimum space. It doesn't even get Resize event. And it's not about changing pages. I don't even change them, I'm always on 1st page. The problem is only aboutQDialog
resizing.Perhaps, I've missed something, sorry.
-
"MyDialog" = QEvent::Resize // <-- here it grows before showing the frame "frame" = QEvent::Move "frame" = QEvent::Resize "frame" = QEvent::Show "frame" = QEvent::ShowToParent "MyDialog" = QEvent::UpdateRequest "MyDialog" = QEvent::Paint "frame" = QEvent::Paint "MyDialog" = QEvent::LayoutRequest "frame" = QEvent::UpdateLater "MyDialog" = QEvent::UpdateRequest "MyDialog" = QEvent::Paint "frame" = QEvent::Paint "MyDialog" = QEvent::UpdateRequest "MyDialog" = QEvent::Paint "MyDialog" = QEvent::UpdateRequest "frame" = QEvent::Hide "frame" = QEvent::HideToParent "MyDialog" = QEvent::Resize // here it shrinks after the frame is hidden
"MyDialogWithStackedWidget" = QEvent::Resize // <-- grows before showing the frame "frame" = QEvent::Move "frame" = QEvent::Resize "frame" = QEvent::Show "frame" = QEvent::ShowToParent "MyDialogWithStackedWidget" = QEvent::UpdateRequest "MyDialogWithStackedWidget" = QEvent::Paint "stackedWidget" = QEvent::Paint "frame" = QEvent::Paint "stackedWidget" = QEvent::LayoutRequest "MyDialogWithStackedWidget" = QEvent::LayoutRequest "frame" = QEvent::UpdateLater "MyDialogWithStackedWidget" = QEvent::UpdateRequest "MyDialogWithStackedWidget" = QEvent::Paint "stackedWidget" = QEvent::Paint "frame" = QEvent::Paint "MyDialogWithStackedWidget" = QEvent::UpdateRequest "MyDialogWithStackedWidget" = QEvent::Paint "stackedWidget" = QEvent::Paint "MyDialogWithStackedWidget" = QEvent::UpdateRequest "frame" = QEvent::Hide "frame" = QEvent::HideToParent "stackedWidget" = QEvent::Resize // <-- only the stacked widget gets resize event and properly shrinks "MyDialogWithStackedWidget" = QEvent::UpdateRequest "MyDialogWithStackedWidget" = QEvent::Paint // the dialog never shrinks after the frame is hidden (never gets resize event)
-
MyDialog
was getting the resize event after its contents were changed.
InMyDialogWithStackedWidget
the stacked widget now gets that resize event after its contents are changed and it feels like it just doesn't propagate it to its parent, no? -
Okay, I managed to make it work like this:
void MyDialogWithStackedWidget::ShowFrame(bool isShown) { ui->frame->setVisible(isShown); ui->stackedWidget->currentWidget()->adjustSize(); ui->stackedWidget->adjustSize(); adjustSize(); }
And setting size policies for current active page (
stackedWidget->currentWidget
), stackWidget and dialog itself to Fixed/Fixed.Perhaps, that's what @JoeCFD meant in the first place and I just didn't get it.