diff --git a/src/base/utils/misc.cpp b/src/base/utils/misc.cpp index 259151fec..f9d93fc12 100644 --- a/src/base/utils/misc.cpp +++ b/src/base/utils/misc.cpp @@ -59,8 +59,10 @@ #else #include #include -#include +#include #include +#include +#include #if (defined(Q_OS_UNIX) && !defined(Q_OS_MAC)) && defined(QT_DBUS_LIB) #include #include @@ -252,11 +254,26 @@ QPoint Utils::Misc::screenCenter(const QWidget *w) { // Returns the QPoint which the widget will be placed center on screen (where parent resides) + if (!w) + return {}; + + QRect r = QGuiApplication::primaryScreen()->availableGeometry(); + const QPoint primaryScreenCenter {(r.x() + (r.width() - w->frameSize().width()) / 2), (r.y() + (r.height() - w->frameSize().height()) / 2)}; + const QWidget *parent = w->parentWidget(); - const QDesktopWidget *desktop = QApplication::desktop(); - const int scrn = desktop->screenNumber(parent); // fallback to `primaryScreen` when parent is invalid - const QRect r = desktop->availableGeometry(scrn); - return {r.x() + (r.width() - w->frameSize().width()) / 2, r.y() + (r.height() - w->frameSize().height()) / 2}; + if (!parent) + return primaryScreenCenter; + + const QWindow *window = parent->window()->windowHandle(); + if (!window) + return primaryScreenCenter; + + const QScreen *screen = window->screen(); + if (!screen) + return primaryScreenCenter; + + r = screen->availableGeometry(); + return {(r.x() + (r.width() - w->frameSize().width()) / 2), (r.y() + (r.height() - w->frameSize().height()) / 2)}; } #endif