Skip to content

Commit

Permalink
ENH: Add ctk::qSetToQStringList
Browse files Browse the repository at this point in the history
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<T>(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<T>(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 <[email protected]>
  • Loading branch information
jcfr and lassoan committed Nov 4, 2021
1 parent 2e12c61 commit 19c36e2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Libs/Core/Testing/Cpp/ctkUtilsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ private slots:

void testQStringListToQSet();
void testQStringListToQSet_data();

void testQSetToQStringList();
void testQSetToQStringList_data();
};

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -272,6 +275,29 @@ void ctkUtilsTester::testQStringListToQSet_data()
<< (QSet<QString>() << "foo" << "bar");
}

// ----------------------------------------------------------------------------
void ctkUtilsTester::testQSetToQStringList()
{
QFETCH(QSet<QString>, input);
QFETCH(QStringList, output);

QStringList current = ctk::qSetToQStringList(input);
current.sort();

QCOMPARE(current, output);
}

// ----------------------------------------------------------------------------
void ctkUtilsTester::testQSetToQStringList_data()
{
QTest::addColumn< QSet<QString> >("input");
QTest::addColumn< QStringList >("output");

QTest::newRow("0")
<< (QSet<QString>() << "foo" << "bar")
<< (QStringList() << "bar" << "foo");
}

// ----------------------------------------------------------------------------
#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
Q_DECLARE_METATYPE (QSet<QString>)
Expand Down
10 changes: 10 additions & 0 deletions Libs/Core/ctkUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ QSet<QString> ctk::qStringListToQSet(const QStringList& list)
#endif
}

//------------------------------------------------------------------------------
QStringList ctk::qSetToQStringList(const QSet<QString>& set)
{
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
return QList<QString>(set.begin(), set.end());
#else
return QStringList::fromSet(set);
#endif
}

//-----------------------------------------------------------------------------
const char *ctkNameFilterRegExp =
"^(.*)\\(([a-zA-Z0-9_.*? +;#\\-\\[\\]@\\{\\}/!<>\\$%&=^~:\\|]*)\\)$";
Expand Down
9 changes: 9 additions & 0 deletions Libs/Core/ctkUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ void CTK_CORE_EXPORT stlVectorToQList(const std::vector<std::string>& vector, QS
/// pre and post Qt 5.14.
QSet<QString> 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<QString>& set);


///
/// \ingroup Core
/// Convert a nameFilter to a list of file extensions:
Expand Down

0 comments on commit 19c36e2

Please sign in to comment.