Qt gets the size or resolution of the screen (desktop)

Qt provides two classes, QDesktopWidget and QScreen, to obtain the screen size. Starting from Qt5, QDesktopWidget is officially deprecated and replaced with QScreen. In Qt 6.0 and later versions, QDesktopWidget has been completely removed from the QtWidgets module.

QDesktopWidget

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();

QScreen Gets the system screen size

#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());  //分辨率大小
}

If there are multiple screens, 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). 

    /**
     * 设置窗体初始化位置及尺寸。
     */
    QScreen* screen = QApplication::primaryScreen();
    QRect rectangle = screen->geometry();
    int width = rectangle.width();
    int height = rectangle.height();
    setGeometry(width / 10, height / 10, width * 3 / 4, height * 4 / 5);

 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 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/weiweiqiao/article/details/133336503