From 866f4819d64d579f67c3bbe1f8a33898cc2e442f Mon Sep 17 00:00:00 2001 From: wangrong Date: Wed, 28 Oct 2026 23:41:05 +0800 Subject: [PATCH] fix: center dialogs on active screen in multi-monitor Wayland MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Root cause: DAbstractDialogPrivate::getParentGeometry() returns parentWidget()->window()->geometry() which is (0,0) on Wayland, causing dialogs to center on primary screen instead of the screen where the parent window resides 2. Fix: override showEvent() in DDBase and MessageBox to use QGuiApplication::screenAt(QCursor::pos()) for correct screen detection, then manually center the dialog on that screen 3. Impact: DDBase covers all its dialog subclasses including CreatePartitionTableDialog; MessageBox covers all message dialogs; no behavior change on single-screen or X11 sessions Log: Fix dialogs appearing on primary screen instead of active screen in multi-monitor Wayland Influence: 1. Test dialog positioning on dual-monitor Wayland with parent window on secondary screen 2. Verify dialog centering on single-screen setup shows no regression 3. Test all DDBase subclass dialogs and MessageBox instances for correct positioning fix: 修复多屏Wayland环境下弹窗显示在主屏的问题 1. 根因:DAbstractDialogPrivate::getParentGeometry() 返回 parentWidget()->window()->geometry(),在Wayland下该值为(0,0)起点, 导致弹窗居中于主屏而非父窗口所在屏幕 2. 方案:在DDBase和MessageBox中重写showEvent(),使用 QGuiApplication::screenAt(QCursor::pos())获取当前屏幕, 手动将弹窗居中到该屏幕 3. 影响:DDBase覆盖其所有子类弹窗包括CreatePartitionTableDialog; MessageBox覆盖所有消息弹窗;单屏环境及X11会话无行为变化 Log: 修复多屏Wayland环境下弹窗显示在主屏而非当前屏幕的问题 Influence: 1. 测试双屏Wayland环境下父窗口在副屏时弹窗定位是否正确 2. 验证单屏环境下弹窗居中行为无回归 3. 测试所有DDBase子类弹窗和MessageBox实例的定位是否正确 PMS: BUG-333047 --- application/widgets/customcontrol/ddbase.cpp | 18 ++++++++++++++++++ application/widgets/customcontrol/ddbase.h | 5 +++++ application/widgets/messagebox.cpp | 18 ++++++++++++++++++ application/widgets/messagebox.h | 5 +++++ 4 files changed, 46 insertions(+) diff --git a/application/widgets/customcontrol/ddbase.cpp b/application/widgets/customcontrol/ddbase.cpp index 1fe9d8bb..c065fe35 100644 --- a/application/widgets/customcontrol/ddbase.cpp +++ b/application/widgets/customcontrol/ddbase.cpp @@ -5,7 +5,10 @@ #include "ddbase.h" #include "common.h" +#include #include +#include +#include DDBase::DDBase(QWidget *parent) : DDialog(parent) @@ -33,3 +36,18 @@ void DDBase::keyPressEvent(QKeyEvent *event) DDialog::keyPressEvent(event); } } + +void DDBase::showEvent(QShowEvent *event) +{ + DDialog::showEvent(event); + + QScreen *screen = QGuiApplication::screenAt(QCursor::pos()); + if (!screen && parentWidget()) + screen = QGuiApplication::screenAt(parentWidget()->window()->geometry().center()); + if (!screen) + screen = QGuiApplication::primaryScreen(); + if (screen) { + QRect screenGeo = screen->geometry(); + move(screenGeo.center() - rect().center()); + } +} diff --git a/application/widgets/customcontrol/ddbase.h b/application/widgets/customcontrol/ddbase.h index b73267bf..8f628aaa 100644 --- a/application/widgets/customcontrol/ddbase.h +++ b/application/widgets/customcontrol/ddbase.h @@ -34,6 +34,11 @@ class DDBase : public DDialog * @param event事件 */ void keyPressEvent(QKeyEvent *event) override; + /** + * @brief 显示事件,Wayland下手动居中到当前屏幕 + * @param event事件 + */ + void showEvent(QShowEvent *event) override; }; #endif // DDBASE_H diff --git a/application/widgets/messagebox.cpp b/application/widgets/messagebox.cpp index 8e3bbeea..d80993cf 100644 --- a/application/widgets/messagebox.cpp +++ b/application/widgets/messagebox.cpp @@ -12,7 +12,10 @@ #include #include +#include #include +#include +#include MessageBox::MessageBox(QWidget *parent) : DDialog(parent) { @@ -192,4 +195,19 @@ void MessageBox::keyPressEvent(QKeyEvent *event) // qDebug() << "MessageBox::keyPressEvent completed."; } +void MessageBox::showEvent(QShowEvent *event) +{ + DDialog::showEvent(event); + + QScreen *screen = QGuiApplication::screenAt(QCursor::pos()); + if (!screen && parentWidget()) + screen = QGuiApplication::screenAt(parentWidget()->window()->geometry().center()); + if (!screen) + screen = QGuiApplication::primaryScreen(); + if (screen) { + QRect screenGeo = screen->geometry(); + move(screenGeo.center() - rect().center()); + } +} + diff --git a/application/widgets/messagebox.h b/application/widgets/messagebox.h index ea9bd679..60204742 100644 --- a/application/widgets/messagebox.h +++ b/application/widgets/messagebox.h @@ -85,6 +85,11 @@ public slots: * @param event事件 */ void keyPressEvent(QKeyEvent *event) override; + /** + * @brief 显示事件,Wayland下手动居中到当前屏幕 + * @param event事件 + */ + void showEvent(QShowEvent *event) override; private: DProgressBar *m_progressBar; // 进度条