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.6 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>
This commit is contained in:
Volker Hilsheimer 2023-09-11 12:25:00 +02:00
parent f07efb4819
commit 6c90aa029b
2 changed files with 26 additions and 1 deletions

View File

@ -1537,7 +1537,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()
{