test: adapt unit tests for Qt5/Qt6 dual compatibility#205
Conversation
There was a problem hiding this comment.
Sorry @pengfeixx, you have reached your weekly rate limit of 500000 diff characters.
Please try again later or upgrade to continue using Sourcery
Add qteventcompat.h with QT_COMPAT_MOUSE_EVENT and QT_COMPAT_WHEEL_EVENT macros to handle QMouseEvent and QWheelEvent constructor signature changes between Qt5/Qt6. Update CMakeLists.txt to auto-detect Qt version (5 or 6), upgrade C++ standard to C++17, use modern cmake target-based linking, and remove deprecated qt5_use_modules. 添加qteventcompat.h兼容头文件,处理Qt5/Qt6鼠标和滚轮 事件构造函数签名差异;更新CMake构建系统自动检测Qt版本, 升级C++标准至C++17,使用现代CMake链接方式。 Log: 单元测试适配Qt5/Qt6双版本兼容 Influence: 测试代码可同时兼容Qt5和Qt6构建环境,无需 手动修改事件构造代码即可在不同Qt版本上编译运行。
c8dacf2 to
ed61fea
Compare
deepin pr auto review你好!我是CodeGeeX。我已经仔细审查了你提供的 Git Diff。本次修改的主要目的是将测试工程从 Qt5 升级兼容到 Qt6,同时调整了 C++ 标准、CMake 最低版本,并引入了自定义宏来解决 Qt5/Qt6 事件构造函数的兼容性问题。 总体来说,这是一次结构清晰、目标明确的重构。但在语法逻辑、代码质量和安全性方面,我发现了几个需要特别注意和改进的地方,尤其是 CMake 中的严重拼写错误和 C++ 宏的潜在风险。 以下是详细的审查意见: 一、 语法与逻辑1. CMake 严重拼写错误导致链接标志失效 - set(CMAKE_L_FLAGS "-g -O1 -fprofile-arcs -ftest-coverage")
+ set(CMAKE_L_FLAGS "-fsanitize=undefined,address,leak -fno-omit-frame-pointer -g -O1 -fprofile-arcs -ftest-coverage")问题: set(CMAKE_C_FLAGS "-fsanitize=undefined,address,leak -fno-omit-frame-pointer -g -O1 -fprofile-arcs -ftest-coverage")
set(CMAKE_CXX_FLAGS "-fsanitize=undefined,address,leak -fno-omit-frame-pointer -g -O1 -fprofile-arcs -ftest-coverage")2. CMake 中 DTK 版本降级回退逻辑缺陷 if(Qt6_FOUND)
set(QT_VERSION_MAJOR 6)
set(DTK_VERSION_MAJOR 6)
else()
find_package(Qt5 REQUIRED COMPONENTS Core Widgets Gui Test)
set(QT_VERSION_MAJOR 5)
set(DTK_VERSION_MAJOR "")
endif()问题:当找不到 Qt6 时, else()
find_package(Qt5 REQUIRED COMPONENTS Core Widgets Gui Test)
set(QT_VERSION_MAJOR 5)
set(DTK_VERSION_MAJOR 5) # 明确指定版本
endif()3. CMake 中 Qt5 查找组件缺失 else()
find_package(Qt5 REQUIRED COMPONENTS Core Widgets Gui Test)问题:在下方定义 二、 代码质量1. 宏 #define QT_COMPAT_MOUSE_EVENT(name, type, pos, button, buttons, modifiers) \
QMouseEvent name((type), (pos), (pos), (button), (buttons), (modifiers))问题:使用宏声明局部变量是一种反模式,它破坏了作用域规则,容易引发变量名冲突,且在调试时宏展开可能会导致困惑。 // 替代宏的 inline 函数方案
namespace QtEventCompat {
inline QMouseEvent createMouseEvent(QEvent::Type type, const QPointF &pos,
Qt::MouseButton button, Qt::MouseButtons buttons,
Qt::KeyboardModifiers modifiers) {
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
return QMouseEvent(type, pos, pos, button, buttons, modifiers);
#else
return QMouseEvent(type, pos, button, buttons, modifiers);
#endif
}
inline QWheelEvent createWheelEvent(const QPointF &pos, int delta,
Qt::MouseButton button, Qt::KeyboardModifiers modifiers) {
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
return QWheelEvent(pos, pos, QPoint(), QPoint(0, delta),
Qt::MouseButtons(button), modifiers, Qt::NoScrollPhase, false);
#else
return QWheelEvent(pos, delta, button, modifiers);
#endif
}
} // namespace QtEventCompat调用方式从: 2. 冗余的注释清理 - #set(CMAKE_CXX_FLAGS "-g -O1 -fprofile-arcs -ftest-coverage")
set(CMAKE_CXX_FLAGS "-g -O1 -fprofile-arcs -ftest-coverage")
- #set(CMAKE_C_FLAGS "-g -O1 -fprofile-arcs -ftest-coverage")
- set(CMAKE_L_FLAGS "-g -O1 -fprofile-arcs -ftest-coverage")改进建议:删除被注释掉的旧代码,保持 CMakeLists.txt 的整洁。既然已经使用了 Git,历史记录由 Git 保管,无需在代码中保留注释掉的废代码。 三、 代码性能本次修改对运行时性能没有明显的负面影响。但有两点可以微调:
四、 代码安全1. C++ 标准库内部破坏风险(已修复,值得表扬) // Pre-include standard headers before #define private public to avoid
// breaking C++ standard library internals (e.g. std::basic_stringbuf).
#include <sstream>
#define protected public
#define private public评价:这是一个非常棒且关键的修复!在 C++ 中 2.
3. 版权年份的修改 -// SPDX-FileCopyrightText: 2020 - 2022 UnionTech Software Technology Co., Ltd.
+// SPDX-FileCopyrightText: 2020-2026 UnionTech Software Technology Co., Ltd.问题:将版权年份改为 2026(未来年份)。通常开源协议和版权声明中的年份应该是作品创建或发布的真实年份,写未来年份在法律上并不被推荐,也无实际保护意义。 总结与核心修改代码示例请务必修复 CMake 中的全角字符 // SPDX-FileCopyrightText: 2020-2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#ifndef QTEVENTCOMPAT_H
#define QTEVENTCOMPAT_H
#include <QtGlobal>
#include <QEvent>
#include <QPointF>
#include <QPoint>
#include <Qt>
#include <QWheelEvent>
#include <QMouseEvent>
namespace QtEventCompat {
inline QMouseEvent createMouseEvent(QEvent::Type type, const QPointF &pos,
Qt::MouseButton button, Qt::MouseButtons buttons,
Qt::KeyboardModifiers modifiers) {
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
return QMouseEvent(type, pos, pos, button, buttons, modifiers);
#else
return QMouseEvent(type, pos, button, buttons, modifiers);
#endif
}
// 针对 QPoint 的重载,方便原有代码直接传入 QPoint
inline QMouseEvent createMouseEvent(QEvent::Type type, const QPoint &pos,
Qt::MouseButton button, Qt::MouseButtons buttons,
Qt::KeyboardModifiers modifiers) {
return createMouseEvent(type, QPointF(pos), button, buttons, modifiers);
}
inline QWheelEvent createWheelEvent(const QPointF &pos, int delta,
Qt::MouseButton button, Qt::KeyboardModifiers modifiers) {
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
return QWheelEvent(pos, pos, QPoint(), QPoint(0, delta),
Qt::MouseButtons(button), modifiers, Qt::NoScrollPhase, false);
#else
return QWheelEvent(pos, delta, button, modifiers);
#endif
}
} // namespace QtEventCompat
#endif // QTEVENTCOMPAT_H调用方式修改示例: // 旧方式:
// QT_COMPAT_MOUSE_EVENT(mousePressEvent, QEvent::MouseButtonPress, QPointF(5, 5), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
// 新方式:
QMouseEvent mousePressEvent = QtEventCompat::createMouseEvent(QEvent::MouseButtonPress, QPointF(5, 5), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);希望这些审查意见对你有所帮助!如果有任何疑问,欢迎继续讨论。 |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: lzwind, pengfeixx The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
/merge |
Add qteventcompat.h with QT_COMPAT_MOUSE_EVENT and QT_COMPAT_WHEEL_EVENT macros to handle QMouseEvent and QWheelEvent constructor signature changes between Qt5/Qt6.
Update CMakeLists.txt to auto-detect Qt version (5 or 6), upgrade C++ standard to C++17, use modern cmake target-based linking, and remove deprecated qt5_use_modules.
添加qteventcompat.h兼容头文件,处理Qt5/Qt6鼠标和滚轮
事件构造函数签名差异;更新CMake构建系统自动检测Qt版本,
升级C++标准至C++17,使用现代CMake链接方式。
Log: 单元测试适配Qt5/Qt6双版本兼容
Influence: 测试代码可同时兼容Qt5和Qt6构建环境,无需
手动修改事件构造代码即可在不同Qt版本上编译运行。