QDockWidget: ignore close event if DockWidgetClosable is not set

[ChangeLog][QtWidgets][QDockWidget] A floating dockwidget that doesn't
have the DockWidgetClosable feature flag set can no longer be closed by
a call to QWidget::close or a corresponding keyboard shortcut (such as
Alt+F4).

Fixes: QTBUG-116752
Pick-to: 6.5
Change-Id: I7859a2eed11f0e4ee013f7f56611e282e9bcae9a
Reviewed-by: Santhosh Kumar <santhosh.kumar.selvaraj@qt.io>
Reviewed-by: Doris Verria <doris.verria@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
(cherry picked from commit 6c90aa029bb65dc5357ac7a26738e711c97732a2)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Volker Hilsheimer 2023-09-11 12:25:00 +02:00 committed by Qt Cherry-pick Bot
parent 0098309003
commit eb1a6263ce
2 changed files with 26 additions and 1 deletions

View File

@ -1531,7 +1531,13 @@ void QDockWidget::closeEvent(QCloseEvent *event)
Q_D(QDockWidget);
if (d->state)
d->endDrag(true);
QWidget::closeEvent(event);
// For non-closable widgets, don't allow closing, except when the mainwindow
// is hidden, as otherwise an application wouldn't be able to be shut down.
const QMainWindow *win = qobject_cast<QMainWindow*>(parentWidget());
const bool canClose = (d->features & DockWidgetClosable)
|| (!win || !win->isVisible());
event->setAccepted(canClose);
}
/*! \reimp */

View File

@ -73,6 +73,7 @@ private slots:
// test closing and deleting consistency
void closeAndDelete();
void closeUnclosable();
// test save and restore consistency
void saveAndRestore();
@ -1585,6 +1586,24 @@ void tst_QDockWidget::closeAndDelete()
#endif // QT_BUILD_INTERNAL
}
void tst_QDockWidget::closeUnclosable()
{
QDockWidget *dockWidget = new QDockWidget("dock");
dockWidget->setWidget(new QScrollArea);
dockWidget->setFeatures(QDockWidget::DockWidgetFloatable);
QMainWindow mw;
mw.addDockWidget(Qt::TopDockWidgetArea, dockWidget);
mw.show();
QVERIFY(QTest::qWaitForWindowExposed(&mw));
dockWidget->setFloating(true);
QCOMPARE(dockWidget->close(), false);
mw.close();
QCOMPARE(dockWidget->close(), true);
}
// Test dock area permissions
void tst_QDockWidget::dockPermissions()
{