封装接口如下
void getScreenSize(int argc, char *argv[],QHash<QString,QSize> &screenSizes){
QApplication app(argc, argv);
const QList<QScreen *> screens = QGuiApplication::screens();
for(auto screen : screens){
Qt::ScreenOrientation orientation = screen->orientation();
QSize screenSize = screen->size();
qreal logicalDpiX = screen->logicalDotsPerInchX();
qreal logicalDpiY = screen->logicalDotsPerInchY();
qreal scaleX = logicalDpiX / 96.0;
qreal scaleY = logicalDpiY / 96.0;
qDebug() <<screen->name() << "显示器分辨率:" << screenSize
<< "屏幕缩放比例 X:" << scaleX << "屏幕缩放比例 Y:" << scaleY << " 屏幕方向:"<<orientation;
screenSizes[screen->name()] = screenSize;
}
}
测试代码
int main(int argc, char *argv[])
{
QHash<QString,QSize> screenSizes;
getScreenSize(argc,argv,screenSizes);
//开启这个后 屏幕分辨率和缩放比会发生变化
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
getScreenSize(argc,argv,screenSizes);
return -1;
}
运行结果
"\\\\.\\DISPLAY1" 显示器分辨率: QSize(3840, 2160) 屏幕缩放比例 X: 1.5 屏幕缩放比例 Y: 1.5 屏幕方向: Qt::LandscapeOrientation
"\\\\.\\DISPLAY2" 显示器分辨率: QSize(3840, 2160) 屏幕缩放比例 X: 1.5 屏幕缩放比例 Y: 1.5 屏幕方向: Qt::LandscapeOrientation
"\\\\.\\DISPLAY1" 显示器分辨率: QSize(1920, 1080) 屏幕缩放比例 X: 1 屏幕缩放比例 Y: 1 屏幕方向: Qt::LandscapeOrientation
"\\\\.\\DISPLAY2" 显示器分辨率: QSize(1920, 1080) 屏幕缩放比例 X: 1 屏幕缩放比例 Y: 1 屏幕方向: Qt::LandscapeOrientation