【Qt】Qt编译是出现Warning QT_DEVICE_PIXEL_RATIO is deprecated

文章描述了在Qt5.12.12环境下,Windows10系统编译时遇到QT_DEVICE_PIXEL_RATIO被弃用的警告,通过删除环境变量并重启电脑解决了问题。验证后,编译过程无警告并成功执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

00. 目录

01. 问题描述

Qt 5.12.12 Windows10系统

编译时出现如下问题:

20:27:35: Starting E:\Program Data\Qt5\build-1Hello-Desktop_Qt_5_12_12_MinGW_64_bit-Debug\debug\1Hello.exe ...
Warning: QT_DEVICE_PIXEL_RATIO is deprecated. Instead use:
   QT_AUTO_SCREEN_SCALE_FACTOR to enable platform plugin controlled per-screen factors.
   QT_SCREEN_SCALE_FACTORS to set per-screen factors.
   QT_SCALE_FACTOR to set the application global scale factor.
20:27:46: E:\Program Data\Qt5\build-1Hello-Desktop_Qt_5_12_12_MinGW_64_bit-Debug\debug\1Hello.exe exited with code 0

02. 问题分析

Warning: QT_DEVICE_PIXEL_RATIO is deprecated, Qt设备像素缩放已经被弃用。

03. 问题解决

删除 用户和系统的 环境变量。
“QT_DEVICE_PIXEL_RATIO”,键为"auto" 。重启电脑。
在这里插入图片描述

系统变量和Administrator的用户变量都需要删除。

04. 问题验证

解决问题之后重新编译

20:42:34: 为项目1Hello执行步骤 ...
20:42:34: 配置没有改变, 跳过 qmake 步骤。
20:42:34: 正在启动 "D:\Qt\Qt5.12.12\Tools\mingw730_64\bin\mingw32-make.exe" -j8

D:/Qt/Qt5.12.12/Tools/mingw730_64/bin/mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory 'E:/Program Data/Qt5/build-1Hello-Desktop_Qt_5_12_12_MinGW_64_bit-Debug'
mingw32-make[1]: Nothing to be done for 'first'.
mingw32-make[1]: Leaving directory 'E:/Program Data/Qt5/build-1Hello-Desktop_Qt_5_12_12_MinGW_64_bit-Debug'
20:42:36: 进程"D:\Qt\Qt5.12.12\Tools\mingw730_64\bin\mingw32-make.exe"正常退出。
20:42:36: Elapsed time: 00:01.

05. 附录

`QT_DEPRECATED_SINCE` 是 Qt 提供的一个宏,用于标记某个类、函数或方法在某个特定版本中**已被弃用(deprecated)**。它的作用是帮助开发者识别哪些 API 已经过时,应该避免使用,并提示使用替代的新 API。 --- ## ✅ 宏定义与用途 ```cpp QT_DEPRECATED_SINCE(x, y, z) ``` 该宏通常用于标记某个函数或类从 Qt 版本 `x.y.z` 开始被弃用。 例如: ```cpp QT_DEPRECATED_SINCE(5, 15, 0) void oldFunction(); ``` 表示 `oldFunction()` 函数从 **Qt 5.15.0** 开始被弃用。 --- ## ✅ 使用方式 你可以在函数声明或类定义中使用该宏,以提醒开发者此函数已被弃用: ### 示例:函数弃用 ```cpp #include <QtGlobal> class MyClass { public: void newFunction() { qDebug() << "Use newFunction instead."; } QT_DEPRECATED_SINCE(6, 0, 0) void oldFunction() { newFunction(); // 调用新函数 } }; ``` ### 示例:类弃用 ```cpp class QT_DEPRECATED_SINCE(5, 15, 0) OldClass { public: void doSomething() { qDebug() << "This class is deprecated."; } }; ``` --- ## ✅ 编译器警告 使用 `QT_DEPRECATED_SINCE` 后,当你调用被弃用的函数或创建被弃用的类对象时,编译器会发出警告(取决于编译器支持和 Qt 的配置)。 例如: ``` warning: 'void oldFunction()' is deprecated: This function was deprecated in Qt 6.0.0. ``` --- ## ✅ 与其它 Qt 弃用宏的区别 | 宏 | 说明 | |----|------| | `QT_DEPRECATED_SINCE(major, minor, patch)` | 标记从某个具体版本开始弃用 | | `QT_DEPRECATED_X("reason")` | 弃用并建议使用替代项,`reason` 为替代说明 | | `QT_DEPRECATED` | 标记为弃用,不指定具体版本 | | `Q_DECL_DEPRECATED` | 旧版本宏,用于标记函数为弃用 | --- ## ✅ 示例:结合 QT_DEPRECATED_X 使用 ```cpp QT_DEPRECATED_X("Use newFunction()") void oldFunction(); ``` 编译器会提示: ``` warning: 'void oldFunction()' is deprecated: Use newFunction() ``` --- ## ✅ 实际应用示例 假设你有一个绘图类,旧的绘图方法已被弃用: ```cpp class Painter { public: void drawCircle(int x, int y, int radius) { qDebug() << "Drawing circle at (" << x << "," << y << ") with radius " << radius; } QT_DEPRECATED_SINCE(6, 2, 0) QT_DEPRECATED_X("Use drawCircle(int x, int y, int radius)") void drawCircleOld(int x, int y, int radius) { drawCircle(x, y, radius); } }; ``` --- ## ✅ 为什么需要弃用机制? 1. **维护兼容性**:不立即删除旧 API,避免破坏现有代码。 2. **鼓励使用新 API**:通过弃用提示,引导开发者使用更安全、更高效的新接口。 3. **清理代码库**:在后续版本中逐步删除已弃用的 API,保持 Qt 的轻量化和现代化。 --- ## ✅ 总结 - `QT_DEPRECATED_SINCE(x, y, z)` 用于标记某个函数或类从指定 Qt 版本开始弃用。 - 使用它可以提高代码可维护性,避免使用过时 API。 - 配合 `QT_DEPRECATED_X()` 可以提供替代建议。 - 在开发库时,应合理使用弃用宏,引导用户迁移到新接口。 --- ##
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沧海一笑-dj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值