来自云龙湖轮廓分明的月亮 发表于 2024-12-21 10:56:04

QScreen在Qt5.15与Qt6.8版本下的区别

简述

QScreen主要用于提供与屏幕相关的信息。它可以获取有关显示设备的分辨率、尺寸、DPI(每英寸点数)等信息。本文主要是先容Qt5.15与Qt6环境下,QScreen的差别,以及如何判断高DPI设备。

属性说明

logicalDotsPerInch:
逻辑DPI,每英寸的逻辑点数或像素数。
经过测试,Qt5.15 逻辑DPI结果会被Windows缩放影响,Qt6不被影响。
physicalDotsPerInch:
物理DPI,每英寸的物理点或像素数。此值表示屏幕显示上的像素密度,根据基础系统提供的信息,该值可能并不完全精确。
经过测试,Qt6.8 物理DPI会被Windows缩放影响,Qt5.15不被影响。
availableGeometry:
返回屏幕的可用几何地区。即清除使命栏或其他屏幕边界外的可用地区。
经过测试,Qt6.8 结果会被Windows缩放影响,Qt5.15不被影响。
physicalSize:
获取屏幕的物理尺寸(单位:毫米),即屏幕的现实物理大小。
manufacturer、model、name:
获取屏幕的厂商、模型、名称(如果有的话)。
devicePixelRatio:
返回设备的像素比(即缩放因子),对于高DPI设备,该值大于1。
经过现实测试,通过此参数,Qt6.8能直接判断高DPI设备(需要Windows放大显示来到达最佳显示效果),Qt5.15则无法判断,换了3台电脑(16寸1920*1080笔记本,大概24寸的1920*1080台式机,以及16寸2560*1600笔记本),devicePixelRatio数值一直是1。
refreshRate:
获取屏幕的革新率(单位:Hz),表示屏幕每秒钟革新的次数。
切换革新率时,Qt6.8和Qt5.15均能检测到。
https://i-blog.csdnimg.cn/direct/5e455b307e8e42d49cbe4bb4954a0666.png

获取主屏幕参数

将QScreen中的成员值打印出来,以下是获取主屏幕参数的代码:
#include <QGuiApplication>
#include <QScreen>
#include <QDebug>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    // 获取默认屏幕
    QScreen *screen = QGuiApplication::primaryScreen();
    if (screen) {
      qDebug() << "logicalDotsPerInch: ";
      qDebug() << screen->logicalDotsPerInch();
      qDebug() << screen->logicalDotsPerInchX();
      qDebug() << screen->logicalDotsPerInchY();
      qDebug() << "=============================";
      qDebug() << "physicalDotsPerInch: ";
      qDebug() << screen->physicalDotsPerInch();
      qDebug() << screen->physicalDotsPerInchX();
      qDebug() << screen->physicalDotsPerInchY();
      qDebug() << "=============================";
      qDebug() << "availableGeometry: " << screen->availableGeometry();
      qDebug() << "availableVirtualGeometry: " << screen->availableVirtualGeometry();
      qDebug() << "virtualGeometry: " << screen->virtualGeometry();
      qDebug() << "nativeOrientation: " << screen->nativeOrientation();
      qDebug() << "orientation: " << screen->orientation();
      qDebug() << "primaryOrientation: " << screen->primaryOrientation();
      qDebug() << "depth: " << screen->depth();
      qDebug() << "devicePixelRatio : " << screen->devicePixelRatio();
      qDebug() << "manufacturer: " << screen->manufacturer();
      qDebug() << "model: " << screen->model();
      qDebug() << "name: " << screen->name();
      qDebug() << "physicalSize: " << screen->physicalSize();
      qDebug() << "refreshRate: " << screen->refreshRate();
      qDebug() << "serialNumber: " << screen->serialNumber();
    }

    return app.exec();
}
Qt5.15.2打印结果

    logicalDotsPerInch: 
144
144
144
=============================
physicalDotsPerInch: 
188.749
188.475
189.023
=============================
availableGeometry:  QRect(0,0 2560x1528)
availableVirtualGeometry:  QRect(0,0 2560x1528)
virtualGeometry:  QRect(0,0 2560x1600)
nativeOrientation:  Qt::PrimaryOrientation
orientation:  Qt::LandscapeOrientation
primaryOrientation:  Qt::LandscapeOrientation
depth:  32
devicePixelRatio :  1
manufacturer:  ""
model:  ""
name:  "\\\\.\\DISPLAY1"
physicalSize:  QSizeF(345, 215)
refreshRate:  60
serialNumber:  ""
Qt6.8.1 打印结果

   logicalDotsPerInch: 
96
96
96
=============================
physicalDotsPerInch: 
125.865
125.675
126.055
=============================
availableGeometry:  QRect(0,0 1707x1019)
availableVirtualGeometry:  QRect(0,0 1707x1019)
virtualGeometry:  QRect(0,0 1707x1067)
nativeOrientation:  Qt::PrimaryOrientation
orientation:  Qt::LandscapeOrientation
primaryOrientation:  Qt::LandscapeOrientation
depth:  32
devicePixelRatio :  1.5
manufacturer:  "BOE"
model:  ""
name:  "\\\\.\\DISPLAY1"
physicalSize:  QSizeF(345, 215)
refreshRate:  60.0006
serialNumber:  ""

对比运行结果

Qt5.15和Qt6.8的运行结果不一致。
将Windows缩放由150%下调至125%后,Qt5.15的运行结果显示:逻辑DPI由144变为120;
物理DPI结果不变,缩放因子依旧是1,不会随着Windows缩放而变化。
https://i-blog.csdnimg.cn/direct/9cbbd91a784740a4ac899b6f3c3dfa30.png
而Qt6.8的运行结果显示:逻辑DPI依旧是96,不会随着Windows缩放而发生变化。
物理DPI由125变为了150,缩放因子由1.5变为了1.25,availableGeometry相关的数据也发生了变化。
https://i-blog.csdnimg.cn/direct/2886b043f3bf4e919f985c9b71194b48.png
判断高DPI屏幕

关于判断是否为高DPI屏幕,在Qt5.15环境下需要用 logicalDotsPerInch/96,得到的值为缩放因子(原devicePixelRatio值不可用);而在Qt6.8环境下,可以直接通过缩放因子(devicePixelRatio > 1)判断是否为高DPI屏幕。



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: QScreen在Qt5.15与Qt6.8版本下的区别