From 9c7e177af0f505e409ca67b60911868edc504c63 Mon Sep 17 00:00:00 2001 From: yeshanshan Date: Thu, 24 Jul 2025 20:10:03 +0800 Subject: [PATCH] fix: resolve compilation warnings and Qt6 compatibility issues MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Replaced QScopedPointer with std::unique_ptr for better memory management and consistency 2. Fixed time handling in DRecentManager for Qt6 by using QTimeZone 3. Added missing memory header for smart pointers These changes address compiler warnings and ensure proper functionality across both Qt5 and Qt6 versions, while modernizing the codebase with standard C++ features. fix: 修复编译警告和Qt6兼容性问题 1. 使用std::unique_ptr替代QScopedPointer以获得更好的内存管理和一致性 2. 在DRecentManager中修复Qt6的时间处理问题,使用QTimeZone 3. 添加缺失的memory头文件以支持智能指针 这些修改解决了编译器警告问题,并确保代码在Qt5和Qt6版本中都能正常工作,同 时使用标准C++特性使代码更现代化。 --- src/CMakeLists.txt | 6 ++++++ src/dconfig.cpp | 10 +++++----- src/util/drecentmanager.cpp | 5 +++++ 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 189078d2..41cc25dc 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -25,6 +25,12 @@ find_package(ICU REQUIRED COMPONENTS uc) pkg_check_modules(uchardet REQUIRED uchardet) # end text encoding +# 为不包含实际QObject类的头文件跳过AutoMOC处理 +set_property(SOURCE ../include/base/dsingleton.h PROPERTY SKIP_AUTOMOC ON) +if("${DTK_VERSION_MAJOR}" STREQUAL "6") + set_property(SOURCE ../include/util/dthreadutils.h PROPERTY SKIP_AUTOMOC ON) +endif() + # start base include(base/base.cmake) # end base diff --git a/src/dconfig.cpp b/src/dconfig.cpp index 2d57ee5d..a2c59f60 100644 --- a/src/dconfig.cpp +++ b/src/dconfig.cpp @@ -173,13 +173,13 @@ class Q_DECL_HIDDEN FileBackend : public DConfigBackend if (owner->appId == NoAppId) return true; - QScopedPointer file(new DConfigFile(NoAppId, owner->name, owner->subpath)); + std::unique_ptr file(new DConfigFile(NoAppId, owner->name, owner->subpath)); const bool canFallbackToGeneric = !file->meta()->metaPath(prefix).isEmpty(); if (canFallbackToGeneric) { - QScopedPointer cache(file->createUserCache(getuid())); + std::unique_ptr cache(file->createUserCache(getuid())); if (file->load(prefix) && cache->load(prefix)) { - genericConfigFile.reset(file.take()); - genericConfigCache.reset(cache.take()); + genericConfigFile.reset(file.release()); + genericConfigCache.reset(cache.release()); } } return true; @@ -370,7 +370,7 @@ class Q_DECL_HIDDEN DBusBackend : public DConfigBackend complexType >> list; QVariantList res; res.reserve(list.size()); - for (const auto &item : qAsConst(list)) { + for (const auto &item : std::as_const(list)) { res << decodeQDBusArgument(item); } return res; diff --git a/src/util/drecentmanager.cpp b/src/util/drecentmanager.cpp index 6129d314..ce2f7548 100644 --- a/src/util/drecentmanager.cpp +++ b/src/util/drecentmanager.cpp @@ -13,6 +13,7 @@ #include #if QT_VERSION >= QT_VERSION_CHECK(6,0,0) #include +#include #endif DCORE_BEGIN_NAMESPACE @@ -67,7 +68,11 @@ bool DRecentManager::addItem(const QString &uri, DRecentData &data) QFile file(RECENT_PATH); file.open(QIODevice::ReadWrite | QIODevice::Text); +#if QT_VERSION >= QT_VERSION_CHECK(6,0,0) + QString dateTime = QDateTime::currentDateTime().toTimeZone(QTimeZone::UTC).toString(Qt::ISODate); +#else QString dateTime = QDateTime::currentDateTime().toTimeSpec(Qt::OffsetFromUTC).toString(Qt::ISODate); +#endif QDomDocument doc; if (!doc.setContent(&file)) {