summaryrefslogtreecommitdiffstats
path: root/src/widgets
diff options
context:
space:
mode:
authorYuya Nishihara <yuya.nishihara@qt.io>2020-11-02 17:00:36 +0900
committerYuya Nishihara <yuya.nishihara@qt.io>2020-12-03 14:43:42 +0900
commitd9a651707646722915c5f20b64804252dd041a16 (patch)
tree8f79fe7d6ab4a9244b441e2384bd8b6657c1a0cb /src/widgets
parentd6dcf508a6a751c8c32273112957607b062899e8 (diff)
Dialog: Fall back to transient parent to make dialog window get centered
Unlike QWidget-based application, a dialog displayed in QML app doesn't have QWidget parent, so it would be centered in the current screen. Let's fall back to transient parent window if widgetParent() is missing. It's tempting to rewrite adjustPosition() to not depend on QWidget at all, but that seems not easy. Lookup path of window() and transientParentWindow() is slightly different for example, and QWidget::pos() is used instead of mapToGlobal({0, 0}) if the widget is embedded into a native window. Fixes: QTBUG-63406 Change-Id: If72d90aee8d972240243184de4d3c09d77f704ff Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
Diffstat (limited to 'src/widgets')
-rw-r--r--src/widgets/dialogs/qdialog.cpp15
1 files changed, 14 insertions, 1 deletions
diff --git a/src/widgets/dialogs/qdialog.cpp b/src/widgets/dialogs/qdialog.cpp
index 52976611aa..c416ba9a5c 100644
--- a/src/widgets/dialogs/qdialog.cpp
+++ b/src/widgets/dialogs/qdialog.cpp
@@ -871,17 +871,25 @@ void QDialog::showEvent(QShowEvent *event)
/*! \internal */
void QDialog::adjustPosition(QWidget* w)
{
+ Q_D(QDialog);
+
if (const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme())
if (theme->themeHint(QPlatformTheme::WindowAutoPlacement).toBool())
return;
QPoint p(0, 0);
int extraw = 0, extrah = 0;
- if (w)
+ const QWindow *parentWindow = nullptr;
+ if (w) {
w = w->window();
+ } else {
+ parentWindow = d->transientParentWindow();
+ }
QRect desk;
QScreen *scrn = nullptr;
if (w)
scrn = w->screen();
+ else if (parentWindow)
+ scrn = parentWindow->screen();
else if (QGuiApplication::primaryScreen()->virtualSiblings().size() > 1)
scrn = QGuiApplication::screenAt(QCursor::pos());
else
@@ -918,6 +926,11 @@ void QDialog::adjustPosition(QWidget* w)
pp = w->mapToGlobal(QPoint(0,0));
p = QPoint(pp.x() + w->width()/2,
pp.y() + w->height()/ 2);
+ } else if (parentWindow) {
+ // QTBUG-63406: Widget-based dialog in QML, which has no Widget parent
+ // but a transient parent window.
+ QPoint pp = parentWindow->mapToGlobal(QPoint(0, 0));
+ p = QPoint(pp.x() + parentWindow->width() / 2, pp.y() + parentWindow->height() / 2);
} else {
// p = middle of the desktop
p = QPoint(desk.x() + desk.width()/2, desk.y() + desk.height()/2);