From 7eaa5987c88eac05a2fdace62952cd9ec008070f Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Fri, 25 Sep 2015 12:52:39 +0800 Subject: [PATCH] Fix dialogs didn't position on the correct screen which qBittorrent window is on. Closes #1690, #2474, #3538. --- src/base/utils/misc.cpp | 20 +++++++------------- src/base/utils/misc.h | 2 +- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/src/base/utils/misc.cpp b/src/base/utils/misc.cpp index 79871e80b..48fad0147 100644 --- a/src/base/utils/misc.cpp +++ b/src/base/utils/misc.cpp @@ -219,21 +219,15 @@ void Utils::Misc::shutdownComputer(const ShutdownDialogAction &action) } #ifndef DISABLE_GUI -// Get screen center -QPoint Utils::Misc::screenCenter(QWidget *win) +QPoint Utils::Misc::screenCenter(const QWidget *w) { - int scrn = 0; - const QWidget *w = win->window(); + // Returns the QPoint which the widget will be placed center on screen (where parent resides) - if (w) - scrn = QApplication::desktop()->screenNumber(w); - else if (QApplication::desktop()->isVirtualDesktop()) - scrn = QApplication::desktop()->screenNumber(QCursor::pos()); - else - scrn = QApplication::desktop()->screenNumber(win); - - QRect desk(QApplication::desktop()->availableGeometry(scrn)); - return QPoint((desk.width() - win->frameGeometry().width()) / 2, (desk.height() - win->frameGeometry().height()) / 2); + QWidget *parent = w->parentWidget(); + QDesktopWidget *desktop = QApplication::desktop(); + int scrn = desktop->screenNumber(parent); // fallback to `primaryScreen` when parent is invalid + QRect r = desktop->availableGeometry(scrn); + return QPoint(r.x() + (r.width() - w->frameSize().width()) / 2, r.y() + (r.height() - w->frameSize().height()) / 2); } #endif diff --git a/src/base/utils/misc.h b/src/base/utils/misc.h index 856ca52b2..cf733e9a7 100644 --- a/src/base/utils/misc.h +++ b/src/base/utils/misc.h @@ -102,7 +102,7 @@ namespace Utils void openPath(const QString& absolutePath); void openFolderSelect(const QString& absolutePath); - QPoint screenCenter(QWidget *win); + QPoint screenCenter(const QWidget *w); QSize smallIconSize(); QSize largeIconSize(); #endif