1 #include "screen.h" 2 3 #include <QApplication> 4 #include <QDesktopWidget> 5 #include <QScreen> 6 7 namespace { 8 screenFromNumber(int i)9QScreen *screenFromNumber(int i) 10 { 11 const auto screens = QGuiApplication::screens(); 12 if (i < 0 || i >= screens.size()) 13 return nullptr; 14 return screens[i]; 15 } 16 17 } // namespace 18 screenCount()19int screenCount() 20 { 21 return QGuiApplication::screens().size(); 22 } 23 screenNumberAt(const QPoint & pos)24int screenNumberAt(const QPoint &pos) 25 { 26 #if QT_VERSION >= QT_VERSION_CHECK(5,11,0) 27 auto screen = QGuiApplication::screenAt(pos); 28 if (screen == nullptr) 29 screen = QGuiApplication::primaryScreen(); 30 return QGuiApplication::screens().indexOf(screen); 31 #else 32 return QApplication::desktop()->screenNumber(pos); 33 #endif 34 } 35 screenGeometry(int i)36QRect screenGeometry(int i) 37 { 38 #if QT_VERSION >= QT_VERSION_CHECK(5,11,0) 39 auto screen = screenFromNumber(i); 40 return screen ? screen->geometry() : QRect(); 41 #else 42 return QApplication::desktop()->screenGeometry(i); 43 #endif 44 } 45 screenAvailableGeometry(const QPoint & pos)46QRect screenAvailableGeometry(const QPoint &pos) 47 { 48 #if QT_VERSION >= QT_VERSION_CHECK(5,11,0) 49 auto screen = QGuiApplication::screenAt(pos); 50 if (screen == nullptr) { 51 screen = screenFromNumber(0); 52 if (screen == nullptr) 53 return QRect(); 54 } 55 56 const QRect g = screen->availableGeometry(); 57 const qreal ratio = screen->devicePixelRatio(); 58 return QRect(g.topLeft() * ratio, g.bottomRight() * ratio); 59 #else 60 return QApplication::desktop()->availableGeometry(pos); 61 #endif 62 } 63