目次
1. QDesktopWidget はシステムの画面サイズを取得します
QDesktopWidget は、ユーザー ウィンドウ内のウィンドウの位置とアプリケーション ウィンドウの位置を自動的に返すことができる詳細な位置情報を提供します。
QDesktopWidget* pDesktopWidget = QApplication::desktop();
//获取可用桌面大小
QRect deskRect = QApplication::desktop()->availableGeometry();
//获取主屏幕分辨率
QRect screenRect = QApplication::desktop()->screenGeometry();
//获取屏幕数量
int nScreenCount = QApplication::desktop()->screenCount();
Qt5 以降、QDesktopWidget の使用は公式に推奨されておらず、QScreen に変更されています。
Qt 6.0 以降では、QDesktopWidget は QtWidgets モジュールから完全に削除されました。
2. QScreen はシステムの画面サイズを取得します
Qt5 以降、QDesktopWidget の使用は公式に推奨されておらず、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()); //分辨率大小
}
注: マルチスクリーンの場合、各スクリーンの rect が異なり、開始座標が異なります. 1 番目のスクリーンの開始座標は (0, 0) であり、2 番目のスクリーンの開始座標は (1920, 0)
3. geometry() と 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() は、画面のサイズ、つまり、画面下部のツールバーを含む画面解像度のサイズ (1090*1080) を返します。
- availableGeometry() は、画面下のツールバーを除いた、利用可能な画面のサイズを返します (1090*1040)