From 19c36e250e5dc31c67a053f8bcba23e3d4ded67a Mon Sep 17 00:00:00 2001 From: Jean-Christophe Fillion-Robin Date: Wed, 3 Nov 2021 18:35:05 -0400 Subject: [PATCH] ENH: Add ctk::qSetToQStringList This function is useful to support building against Qt>=5.14 and avoid warnings like the following: /path/to/S/Base/QTApp/qSlicerApplicationHelper.txx:169:64: warning: 'fromSet' is deprecated: Use QList(set.begin(), set.end()) instead. [-Wdeprecated-declarations] QStringList failedToBeInstantiatedModuleNames = QStringList::fromSet( ^ /path/to/Support/Qt/5.15.2/clang_64/lib/QtCore.framework/Headers/qlist.h:410:5: note: 'fromSet' has been explicitly marked deprecated here QT_DEPRECATED_VERSION_X_5_14("Use QList(set.begin(), set.end()) instead.") ^ /path/to/Support/Qt/5.15.2/clang_64/lib/QtCore.framework/Headers/qglobal.h:366:45: note: expanded from macro 'QT_DEPRECATED_VERSION_X_5_14' # define QT_DEPRECATED_VERSION_X_5_14(text) QT_DEPRECATED_X(text) ^ /path/to/Support/Qt/5.15.2/clang_64/lib/QtCore.framework/Headers/qglobal.h:294:33: note: expanded from macro 'QT_DEPRECATED_X' # define QT_DEPRECATED_X(text) Q_DECL_DEPRECATED_X(text) ^ /path/to/Support/Qt/5.15.2/clang_64/lib/QtCore.framework/Headers/qcompilerdetection.h:675:55: note: expanded from macro 'Q_DECL_DEPRECATED_X' # define Q_DECL_DEPRECATED_X(text) __attribute__ ((__deprecated__(text))) ^ Co-authored-by: Andras Lasso --- Libs/Core/Testing/Cpp/ctkUtilsTest.cpp | 26 ++++++++++++++++++++++++++ Libs/Core/ctkUtils.cpp | 10 ++++++++++ Libs/Core/ctkUtils.h | 9 +++++++++ 3 files changed, 45 insertions(+) diff --git a/Libs/Core/Testing/Cpp/ctkUtilsTest.cpp b/Libs/Core/Testing/Cpp/ctkUtilsTest.cpp index ec6f7fd457..2ed7025c6d 100644 --- a/Libs/Core/Testing/Cpp/ctkUtilsTest.cpp +++ b/Libs/Core/Testing/Cpp/ctkUtilsTest.cpp @@ -44,6 +44,9 @@ private slots: void testQStringListToQSet(); void testQStringListToQSet_data(); + + void testQSetToQStringList(); + void testQSetToQStringList_data(); }; // ---------------------------------------------------------------------------- @@ -272,6 +275,29 @@ void ctkUtilsTester::testQStringListToQSet_data() << (QSet() << "foo" << "bar"); } +// ---------------------------------------------------------------------------- +void ctkUtilsTester::testQSetToQStringList() +{ + QFETCH(QSet, input); + QFETCH(QStringList, output); + + QStringList current = ctk::qSetToQStringList(input); + current.sort(); + + QCOMPARE(current, output); +} + +// ---------------------------------------------------------------------------- +void ctkUtilsTester::testQSetToQStringList_data() +{ + QTest::addColumn< QSet >("input"); + QTest::addColumn< QStringList >("output"); + + QTest::newRow("0") + << (QSet() << "foo" << "bar") + << (QStringList() << "bar" << "foo"); +} + // ---------------------------------------------------------------------------- #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0)) Q_DECLARE_METATYPE (QSet) diff --git a/Libs/Core/ctkUtils.cpp b/Libs/Core/ctkUtils.cpp index c6b289df0c..1e6c477557 100644 --- a/Libs/Core/ctkUtils.cpp +++ b/Libs/Core/ctkUtils.cpp @@ -89,6 +89,16 @@ QSet ctk::qStringListToQSet(const QStringList& list) #endif } +//------------------------------------------------------------------------------ +QStringList ctk::qSetToQStringList(const QSet& set) +{ +#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) + return QList(set.begin(), set.end()); +#else + return QStringList::fromSet(set); +#endif +} + //----------------------------------------------------------------------------- const char *ctkNameFilterRegExp = "^(.*)\\(([a-zA-Z0-9_.*? +;#\\-\\[\\]@\\{\\}/!<>\\$%&=^~:\\|]*)\\)$"; diff --git a/Libs/Core/ctkUtils.h b/Libs/Core/ctkUtils.h index c46cd3c668..edc0036787 100644 --- a/Libs/Core/ctkUtils.h +++ b/Libs/Core/ctkUtils.h @@ -70,6 +70,15 @@ void CTK_CORE_EXPORT stlVectorToQList(const std::vector& vector, QS /// pre and post Qt 5.14. QSet CTK_CORE_EXPORT qStringListToQSet(const QStringList& list); +/// +/// \ingroup Core +/// \brief Convert a set of strings to a QStringList +/// +/// This method was added so that the same code compiles without deprecation warnings +/// pre and post Qt 5.14. +QStringList CTK_CORE_EXPORT qSetToQStringList(const QSet& set); + + /// /// \ingroup Core /// Convert a nameFilter to a list of file extensions: