Fix closing and showing a window with a native child

Closing a window with a native child results in the native child's
QWidgetWindow being closed. That explicitly calls setVisible(false)
on the child, which will still have the ExplicitShowHide attribute
set from the initial (explicit) show. Even though we then reset
the ExplicitShowHide, the WState_Hidden attribute will still be
set, so Qt considers the window to have been hidden, and not show
it again when the parent becomes visible.

Add a test case.

Fixes: QTBUG-96286
Fixes: QTBUG-79012
Fixes: QTBUG-71519
Change-Id: I482e6d5236c959d82ce66798176b259a3176972c
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Oliver Wolff <oliver.wolff@qt.io>
bb10
Volker Hilsheimer 2021-09-23 17:16:13 +02:00
parent 5d17487723
commit 51300566ff
2 changed files with 51 additions and 2 deletions

View File

@ -78,10 +78,13 @@ public:
// explicitly and not because the parent window in this case made it hidden.
// In which case do not automatically show the widget when the parent
// window is shown.
const bool wasExplicitShowHide = widget->testAttribute(Qt::WA_WState_ExplicitShowHide);
const bool wasHidden = widget->testAttribute(Qt::WA_WState_Hidden);
QWidgetPrivate::get(widget)->setVisible(visible);
if (!wasHidden)
widget->setAttribute(Qt::WA_WState_ExplicitShowHide, false);
if (wasExplicitShowHide) {
widget->setAttribute(Qt::WA_WState_ExplicitShowHide, wasExplicitShowHide);
widget->setAttribute(Qt::WA_WState_Hidden, wasHidden);
}
} else {
QWindowPrivate::setVisible(visible);
}

View File

@ -243,6 +243,7 @@ private slots:
void winIdChangeEvent();
void persistentWinId();
void showNativeChild();
void closeAndShowWithNativeChild();
void transientParent();
void qobject_castInDestroyedSlot();
@ -4773,6 +4774,51 @@ void tst_QWidget::showNativeChild()
QVERIFY(QTest::qWaitForWindowExposed(&topLevel));
}
void tst_QWidget::closeAndShowWithNativeChild()
{
bool dontCreateNativeWidgetSiblings = QApplication::testAttribute(Qt::AA_DontCreateNativeWidgetSiblings);
auto resetAttribute = qScopeGuard([&]{
QApplication::setAttribute(Qt::AA_DontCreateNativeWidgetSiblings, dontCreateNativeWidgetSiblings);
});
QApplication::setAttribute(Qt::AA_DontCreateNativeWidgetSiblings);
QWidget topLevel;
QWidget *nativeChild = new QWidget;
nativeChild->setFixedSize(200, 200);
QWidget *nativeHiddenChild = new QWidget;
nativeHiddenChild->setFixedSize(200, 200);
QWidget *normalChild = new QWidget;
normalChild->setFixedSize(200, 200);
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(nativeChild);
layout->addWidget(nativeHiddenChild);
layout->addWidget(normalChild);
topLevel.setLayout(layout);
nativeHiddenChild->hide();
topLevel.show();
QVERIFY(QTest::qWaitForWindowExposed(&topLevel));
nativeChild->winId();
const QSize originalSize = topLevel.size();
topLevel.close();
// all children must have the same state
QCOMPARE(nativeChild->isHidden(), normalChild->isHidden());
QCOMPARE(nativeChild->isVisible(), normalChild->isVisible());
QCOMPARE(nativeChild->testAttribute(Qt::WA_WState_Visible),
normalChild->testAttribute(Qt::WA_WState_Visible));
QCOMPARE(nativeChild->testAttribute(Qt::WA_WState_Hidden),
normalChild->testAttribute(Qt::WA_WState_Hidden));
QCOMPARE(nativeChild->testAttribute(Qt::WA_WState_ExplicitShowHide),
normalChild->testAttribute(Qt::WA_WState_ExplicitShowHide));
topLevel.show();
QVERIFY(QTest::qWaitForWindowExposed(&topLevel));
QCOMPARE(topLevel.size(), originalSize);
}
class ShowHideEventWidget : public QWidget
{
public: