diff --git a/application/widgets/customcontrol/ddbase.cpp b/application/widgets/customcontrol/ddbase.cpp index 1fe9d8b..c065fe3 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 b73267b..8f628aa 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 8e3bbee..d80993c 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 ea9bd67..6020474 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; // 进度条