From 4731f6f157839ce1d4d0e19be8d8c7cb5ebb202f Mon Sep 17 00:00:00 2001 From: yeshanshan Date: Mon, 18 Aug 2025 12:15:12 +0800 Subject: [PATCH] fix: resolve Qt6 compilation warnings and deprecations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Added conditional compilation for Qt5/Qt6 compatibility 2. Replaced deprecated qAsConst with std::as_const for Qt6 3. Added D_IGNORE_DEPRECATIONS flag to suppress deprecation warnings 4. Fixed static_assert removal in dbackdropnode.cpp 5. Added missing Q_UNUSED macro in dquickimageprovider.cpp 6. Cleaned up initialization syntax in dquickwindow_p.h 7. Fixed platform check from IsTreelandPlatform to IsWaylandPlatform 8. Improved memory management in AppLoaderSimulator destructor Influence: 1. Verify application builds successfully with both Qt5 and Qt6 2. Test high DPI functionality on both Qt versions 3. Validate blur effects and window management features 4. Check all QML components render correctly 5. Test palette and color selector functionality 6. Verify shadow provider image generation 7. Test settings container and group visibility fix: 解决Qt6编译警告和废弃API问题 1. 添加Qt5/Qt6兼容性条件编译 2. 将废弃的qAsConst替换为std::as_const(Qt6) 3. 添加D_IGNORE_DEPRECATIONS标志抑制废弃API警告 4. 修复dbackdropnode.cpp中移除的static_assert 5. 在dquickimageprovider.cpp中添加缺失的Q_UNUSED宏 6. 清理dquickwindow_p.h中的初始化语法 7. 将平台检查从IsTreelandPlatform修正为IsWaylandPlatform 8. 改进AppLoaderSimulator析构函数的内存管理 Influence: 1. 验证应用在Qt5和Qt6下都能成功构建 2. 测试两个Qt版本下的高DPI功能 3. 验证模糊效果和窗口管理功能 4. 检查所有QML组件是否正确渲染 5. 测试调色板和颜色选择器功能 6. 验证阴影提供者图像生成 7. 测试设置容器和组可见性 --- examples/exhibition/main.cpp | 2 + examples/qml-inspect/main.cpp | 2 + qt6/src/CMakeLists.txt | 3 + src/CMakeLists.txt | 3 + src/dapploader.cpp | 10 ++- src/dquickwindow.cpp | 10 ++- src/private/dbackdropnode.cpp | 2 - src/private/dquickcontrolpalette.cpp | 12 ++++ src/private/dquickimageprovider.cpp | 1 + src/private/dquickwaterprogressattribute_p.h | 4 +- src/private/dquickwindow_p.h | 4 +- src/private/dsettingscontainer.cpp | 64 +++++++++++++++++--- tests/ut_dapploader.cpp | 3 +- 13 files changed, 102 insertions(+), 18 deletions(-) diff --git a/examples/exhibition/main.cpp b/examples/exhibition/main.cpp index a0b8a8e2e..37288b325 100644 --- a/examples/exhibition/main.cpp +++ b/examples/exhibition/main.cpp @@ -61,7 +61,9 @@ class Object : public QObject { int main(int argc, char **argv) { +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); +#endif QGuiApplication app(argc, argv); app.setOrganizationName("deepin"); diff --git a/examples/qml-inspect/main.cpp b/examples/qml-inspect/main.cpp index 56c71951e..b4eff902a 100644 --- a/examples/qml-inspect/main.cpp +++ b/examples/qml-inspect/main.cpp @@ -9,7 +9,9 @@ int main(int argc, char *argv[]) { +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); +#endif QGuiApplication app(argc, argv); app.setOrganizationName("deepin"); diff --git a/qt6/src/CMakeLists.txt b/qt6/src/CMakeLists.txt index 39ecd29ba..4f593e1ff 100644 --- a/qt6/src/CMakeLists.txt +++ b/qt6/src/CMakeLists.txt @@ -21,6 +21,9 @@ qt_add_qml_module(${LIB_NAME} dtk_extend_target(${LIB_NAME} EnableCov ${ENABLE_COV}) dtk_extend_target(${PLUGIN_NAME} EnableCov ${ENABLE_COV}) +target_compile_definitions(${LIB_NAME} PRIVATE + D_IGNORE_DEPRECATIONS +) qt_add_translations(${LIB_NAME} TS_FILES ${TS_FILES} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 21a579be1..ce8cd5dbc 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -17,6 +17,9 @@ add_library(${LIB_NAME} SHARED ) dtk_extend_target(${LIB_NAME} EnableCov ${ENABLE_COV}) +target_compile_definitions(${LIB_NAME} PRIVATE + D_IGNORE_DEPRECATIONS +) set_target_properties(${LIB_NAME} PROPERTIES VERSION ${CMAKE_PROJECT_VERSION} diff --git a/src/dapploader.cpp b/src/dapploader.cpp index 635b5ae39..bd3cbcae9 100644 --- a/src/dapploader.cpp +++ b/src/dapploader.cpp @@ -40,7 +40,7 @@ Q_LOGGING_CATEGORY(appLoaderLog, "dtk.quick.apploader"); static const QQuickItemPrivate::ChangeTypes changedTypes = QQuickItemPrivate::Geometry; DAppLoader *DAppLoader::self = nullptr; -static inline const bool heightValid(QQuickItemPrivate *item) +static inline bool heightValid(QQuickItemPrivate *item) { #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) return item->heightValid; @@ -246,7 +246,11 @@ bool DAppLoaderPrivate::createObjects(const char *propertyName) void DAppLoaderPrivate::createChildComponents() { auto components = appRootItem->findChildren(QStringLiteral(""), Qt::FindDirectChildrenOnly); +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + for (auto childCom : std::as_const(components)) { +#else for (auto childCom : qAsConst(components)) { +#endif QObject::connect(childCom, SIGNAL(progressChanged(qreal)), q_func(), SLOT(_q_onComponentProgressChanged())); auto asyn = appRootItem->asynchronous() ? DQmlComponentIncubator::Asynchronous : DQmlComponentIncubator::AsynchronousIfNested; DQmlComponentIncubator *incubator = new DQmlComponentIncubator(childCom, this, asyn); @@ -436,7 +440,11 @@ void DAppLoaderPrivate::_q_onComponentProgressChanged() { qreal progress = 0; auto components = appRootItem->findChildren(); +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + for (auto childCom : std::as_const(components) { +#else for (auto childCom : qAsConst(components)) { +#endif progress += childCom->progress(); } diff --git a/src/dquickwindow.cpp b/src/dquickwindow.cpp index d695c7c2d..46bc0bfa8 100644 --- a/src/dquickwindow.cpp +++ b/src/dquickwindow.cpp @@ -226,7 +226,11 @@ void DQuickWindowAttachedPrivate::_q_updateBlurAreaForWindow() QList blurPathList; QVector blurAreaList; +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + for (const DQuickBehindWindowBlur *blur : std::as_const(blurList)) { +#else for (const DQuickBehindWindowBlur *blur : qAsConst(blurList)) { +#endif if (!blur->d_func()->isValidBlur()) continue; @@ -244,7 +248,11 @@ void DQuickWindowAttachedPrivate::_q_updateBlurAreaForWindow() blurSuc = q->setWindowBlurAreaByWM(blurAreaList); } else { // convert to QPainterPath +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + for (const DPlatformHandle::WMBlurArea &area : std::as_const(blurAreaList)) { +#else for (const DPlatformHandle::WMBlurArea &area : qAsConst(blurAreaList)) { +#endif QPainterPath path; path.addRoundedRect(area.x, area.y, area.width, area.height, area.xRadius, area.yRaduis); blurPathList << path; @@ -343,7 +351,7 @@ bool DQuickWindowAttached::isEnabled() const { D_DC(DQuickWindowAttached); return d->handle && (DPlatformHandle::isEnabledDXcb(window()) - || DGuiApplicationHelper::testAttribute(DGuiApplicationHelper::IsTreelandPlatform)); + || DGuiApplicationHelper::testAttribute(DGuiApplicationHelper::IsWaylandPlatform)); } /*! diff --git a/src/private/dbackdropnode.cpp b/src/private/dbackdropnode.cpp index 5670ef570..1973d226b 100644 --- a/src/private/dbackdropnode.cpp +++ b/src/private/dbackdropnode.cpp @@ -145,7 +145,6 @@ class Q_DECL_HIDDEN DataManager : public DataManagerBase } static DataManagerPointer resolve(const DataManagerPointer &other, QQuickWindow *owner) { - static_assert(&Derive::metaObject); Q_ASSERT(owner); if (other && other->owner() == owner) return other; @@ -828,7 +827,6 @@ class Q_DECL_HIDDEN RhiNode : public DBackdropNode { texture->data->pixelSize().height() / float(m_rect.height() * devicePixelRatio)}); rhi->render(renderData->rt.get()); } else { - auto rhi = this->rhi->rhi(); QPointF sourcePos = renderMatrix.map(m_rect.topLeft()) * devicePixelRatio; if (ct) { diff --git a/src/private/dquickcontrolpalette.cpp b/src/private/dquickcontrolpalette.cpp index 4d2d095ba..bf7ca2967 100644 --- a/src/private/dquickcontrolpalette.cpp +++ b/src/private/dquickcontrolpalette.cpp @@ -344,7 +344,11 @@ void DQuickControlColorSelector::findAndSetControlParent() { QQuickItem *parentItem = qobject_cast(parent()); Q_ASSERT(parentItem); +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) for (const QMetaObject::Connection &conn : qAsConst(m_itemParentChangeConnections)) { +#else + for (const QMetaObject::Connection &conn : std::as_const(m_itemParentChangeConnections)) { +#endif disconnect(conn); } m_itemParentChangeConnections.clear(); @@ -1028,7 +1032,11 @@ void DQuickControlColorSelector::recvPaletteColorChanged() auto palette = qobject_cast(sender()); Q_ASSERT(palette); // Maybe the multiple properties is use a same palette. +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + for (const auto &i : std::as_const(m_palettes)) { +#else for (const auto &i : qAsConst(m_palettes)) { +#endif if (i.second != palette) continue; updatePropertyFromName(i.first, palette); @@ -1040,7 +1048,11 @@ void DQuickControlColorSelector::onPaletteDestroyed() auto palette = sender(); Q_ASSERT(palette); // Maybe the multiple properties is use a same palette. +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + for (const auto &i : std::as_const(m_palettes)) { +#else for (const auto &i : qAsConst(m_palettes)) { +#endif if (i.second != palette) continue; setPalette(i.first, nullptr); diff --git a/src/private/dquickimageprovider.cpp b/src/private/dquickimageprovider.cpp index d2283ad74..3c5010d99 100644 --- a/src/private/dquickimageprovider.cpp +++ b/src/private/dquickimageprovider.cpp @@ -432,6 +432,7 @@ static QPainterPath roundedRectPath(const QRectF &rect, const DQuickShadowProvid QImage DQuickShadowProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize) { + Q_UNUSED(requestedSize) ShadowConfig config; QColor color; qreal xOffset; diff --git a/src/private/dquickwaterprogressattribute_p.h b/src/private/dquickwaterprogressattribute_p.h index 03895ecf1..90da3694b 100644 --- a/src/private/dquickwaterprogressattribute_p.h +++ b/src/private/dquickwaterprogressattribute_p.h @@ -56,8 +56,8 @@ class WaterPopAttribute : public QObject qreal sizeRatio() const; - inline qreal yOffset() const; - inline void setYOffset(qreal offset); + qreal yOffset() const; + void setYOffset(qreal offset); Q_SIGNALS: void xSpeedChanged(); diff --git a/src/private/dquickwindow_p.h b/src/private/dquickwindow_p.h index 17d502216..62923efde 100644 --- a/src/private/dquickwindow_p.h +++ b/src/private/dquickwindow_p.h @@ -65,8 +65,8 @@ class DQuickWindowAttachedPrivate : public DTK_CORE_NAMESPACE::DObjectPrivate QPoint explicitShadowOffset; QColor explicitBorderColor; QColor explicitShadowColor; - DPlatformHandle::EffectScenes explicitEffectScene = DPlatformHandle::EffectScenes(0); - DPlatformHandle::EffectTypes explicitEffectType = DPlatformHandle::EffectTypes(0); + DPlatformHandle::EffectScenes explicitEffectScene {}; + DPlatformHandle::EffectTypes explicitEffectType {}; DWindowManagerHelper::WmWindowTypes wmWindowTypes; DWindowManagerHelper::MotifFunctions motifFunctions; diff --git a/src/private/dsettingscontainer.cpp b/src/private/dsettingscontainer.cpp index 50600b829..8f4b99d0c 100644 --- a/src/private/dsettingscontainer.cpp +++ b/src/private/dsettingscontainer.cpp @@ -29,7 +29,13 @@ static constexpr char const *settingsGroupObjectName = "_d_settings_group"; // key: 'group1.group2' static SettingsGroup *groupByKey(const QList groups, const QString &key) { - for (auto group : qAsConst(groups)) { + for (auto group : +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + std::as_const(groups) +#else + qAsConst(groups) +#endif + ) { if (key == group->key()) { return group; } @@ -125,7 +131,13 @@ QVector SettingsContainer::groupList() const { QVector list; QStack stack; - for (auto group : qAsConst(m_groups)) { + for (auto group : +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + std::as_const(m_groups) +#else + qAsConst(m_groups) +#endif + ) { stack.push_back(group); while (!stack.isEmpty()) { auto group = stack.pop(); @@ -133,7 +145,13 @@ QVector SettingsContainer::groupList() const auto children = *static_cast*>(group->children().data); // keep order when it's declaration. std::reverse(children.begin(), children.end()); - for (auto childGroup : qAsConst(children)) { + for (auto childGroup : +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + std::as_const(children) +#else + qAsConst(children) +#endif + ) { stack.push(childGroup); } list.push_back(group); @@ -176,7 +194,13 @@ bool SettingsContainer::groupVisible(const QString &key) const void SettingsContainer::resetSettings() { - for (auto group : qAsConst(m_groups)) { + for (auto group : +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + std::as_const(m_groups) +#else + qAsConst(m_groups) +#endif + ) { QList gs; gs.append(group); @@ -440,10 +464,22 @@ void SettingsGroup::setBackground(QQmlComponent *background) void SettingsGroup::setConfig(QObject *config) { - for (auto childGroup : qAsConst(m_children)) { + for (auto childGroup : +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + std::as_const(m_children) +#else + qAsConst(m_children) +#endif + ) { childGroup->setConfig(config); } - for (auto option : qAsConst(m_options)) { + for (auto option : +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + std::as_const(m_options) +#else + qAsConst(m_options) +#endif + ) { option->setConfig(config); } } @@ -584,7 +620,13 @@ class SettingsCompositor for (int i = 0; i < groupItems.count(); i++) groupItems[i] = nullptr; - for (auto group : qAsConst(groups)) { + for (auto group : +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + std::as_const(groups) +#else + qAsConst(groups) +#endif + ) { if (group->visible()) { currentGroups.push_back(group); } @@ -852,7 +894,13 @@ QObject *SettingsContentModel::object(int index, QQmlIncubator::IncubationMode i QQuickItem *columnItem = qobject_cast(columnCom.beginCreate(groupContext)); columnItem->setParentItem(groupItem); - for (auto option: qAsConst(*options)) { + for (auto option: +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + std::as_const(*options) +#else + qAsConst(*options) +#endif + ) { if (!option->delegate()) continue; diff --git a/tests/ut_dapploader.cpp b/tests/ut_dapploader.cpp index be7d4ffd7..04dcca8ba 100644 --- a/tests/ut_dapploader.cpp +++ b/tests/ut_dapploader.cpp @@ -98,8 +98,7 @@ class AppLoaderSimulator : public DAppLoader } ~AppLoaderSimulator() { - // don't release app. - d->app.take(); + // app will be automatically cleaned up by QScopedPointer } int load() {