[QT] How to get the size or resolution of the screen (desktop)

1. QDesktopWidget gets the system screen size

QDesktopWidget provides detailed position information, which can automatically return the position of the window in the user window and the position of the application window

QDesktopWidget* pDesktopWidget = QApplication::desktop();
    
//获取可用桌面大小
QRect deskRect = QApplication::desktop()->availableGeometry();
//获取主屏幕分辨率
QRect screenRect = QApplication::desktop()->screenGeometry();
//获取屏幕数量
int nScreenCount = QApplication::desktop()->screenCount();

Starting from Qt5, QDesktopWidget is not officially recommended to be used, and it is changed to QScreen.
In Qt 6.0 and later, QDesktopWidget has been completely removed from the QtWidgets module.

2. QScreen gets the system screen size

Starting from Qt5, QDesktopWidget is not officially recommended to be used, and it is changed to QScreen.

#include<QScreen>
#include<QRect>
 
//单屏幕
QScreen* screen = QGuiApplication::primaryScreen();  //获取主屏幕

//多屏幕
QList<QScreen *> screenList =  QGuiApplication::screens();  //多显示器
QList<QRect *> rectList;
for(int i = 0; i < screenList.size(); i++){
    
    
	rectList.append(screenList.at(i).geometry());  //分辨率大小
}

Note: If it is multi-screen, the rect of each screen is different, and the starting coordinates are different. The starting coordinates of the first screen are (0, 0), and the starting coordinates of the second screen are (1920, 0)

3. The difference between geometry() and availableGeometry()

QScreen* screen = QGuiApplication::primaryScreen();

QRect rect1 = screen->geometry();
qDebug() << "rect1" << rect1.size().width() << rect1.size().height();
qDebug() << rect1.topLeft();
qDebug() << rect1.bottomRight();

QRect rect2 = screen->availableGeometry();
qDebug() << "rect2" << rect2.size().width() << rect2.size().height();
qDebug() << rect2.topLeft();
qDebug() << rect2.bottomRight();
  • geometry() returns the size of the screen, that is, the size of the screen resolution, including the toolbar at the bottom of the screen (1090*1080)
  • availableGeometry() returns the size of the available screen, excluding the toolbar below the screen (1090*1040)

Guess you like

Origin blog.csdn.net/WL0616/article/details/129182751