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均能检测到。

获取主屏幕参数

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

cpp 复制代码
#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缩放而变化。

而Qt6.8的运行结果显示:逻辑DPI依旧是96,不会随着Windows缩放而发生变化。

物理DPI由125变为了150,缩放因子由1.5变为了1.25,availableGeometry相关的数据也发生了变化。

判断高DPI屏幕

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

相关推荐
平常心的技术小牛10 分钟前
Qt-快速上手-QMenuBar
开发语言·qt
Oll Correct2 小时前
Adobe illustrator 案例六:手枪图标绘制
笔记·ui·adobe·illustrator
余额瞒着我当琳2 小时前
C++--深拷贝三件套 + swap + 写时拷贝 + vector 扩容 + reserve
java·开发语言·c++
ShineWinsu4 小时前
对于C++:布隆过滤器(bloomfilter)的详细解析
c++·面试·位图·位运算·布隆过滤器·海量数据·比特位
牛油果子哥q8 小时前
C++内存模型与深浅拷贝万字详解:栈堆静态内存布局、深浅拷贝底层差异、内存泄漏根治、拷贝崩溃踩坑、手写深拷贝实战
java·开发语言·c++
大鹏的NLP博客8 小时前
通用 Linux 嵌入式板端 C/C++ ABI 与 Glibc 依赖治理规范
linux·c++·交叉编译
ysa0510308 小时前
c++常用自带函数用法与注意
c++·笔记·算法
qq_1508419910 小时前
从CVI(C)到Pyside(python)/Qt(C++)/VS(C#)的一点吐槽
c++·qt·c#
我不是疯子是傻子11 小时前
Qt 程序打包部署全攻略:从 windeployqt 到 Inno Setup 的静默安装与版本迭代实战
开发语言·qt
鸿芯微控科技13 小时前
MFC模拟量信号异常怎么排查?0-5V、4-20mA接线与量程换算
c++·5g·mfc·故障排查·质量流量控制器·4-20ma·模拟量信号