Skip to content

Commit

Permalink
qAsConst is deprecated, use std::as_const
Browse files Browse the repository at this point in the history
  • Loading branch information
dfaure-kdab committed Nov 22, 2024
1 parent 231c19d commit 7e7a98b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
11 changes: 8 additions & 3 deletions qt/KDStlContainerAdaptor/test/tst_KDStlContainerAdaptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ void tst_KDStlContainerAdaptor::vectorAdaptorIterators()
QCOMPARE(result, 15);
}

// For Qt5 (without C++17) and Qt6/C++17 compatibility
template <typename T> struct AddConst { typedef const T type; };
template <typename T> constexpr typename AddConst<T>::type &asConst(T &t) noexcept { return t; }
template <typename T> void asConst(const T &&) = delete; // prevent rvalue arguments

void tst_KDStlContainerAdaptor::vectorAdaptorDataAccess()
{
IntVec v{1, 2, 3, 4, 5};
Expand All @@ -80,7 +85,7 @@ void tst_KDStlContainerAdaptor::vectorAdaptorDataAccess()
QCOMPARE(v.constData()[i], expected);
QCOMPARE(v.at(i), expected);
QCOMPARE(v[i], expected);
QCOMPARE(qAsConst(v)[i], expected);
QCOMPARE(asConst(v)[i], expected);
QCOMPARE(v.value(i), expected);
}

Expand All @@ -93,14 +98,14 @@ void tst_KDStlContainerAdaptor::vectorAdaptorDataAccess()
QCOMPARE(v.first(), -1);
v.first() = 123;
QCOMPARE(v.first(), 123);
QCOMPARE(qAsConst(v).first(), 123);
QCOMPARE(asConst(v).first(), 123);
QCOMPARE(v.constFirst(), 123);

v[4] = -1;
QCOMPARE(v.last(), -1);
v.last() = 456;
QCOMPARE(v.last(), 456);
QCOMPARE(qAsConst(v).last(), 456);
QCOMPARE(asConst(v).last(), 456);
QCOMPARE(v.constLast(), 456);
}

Expand Down
4 changes: 4 additions & 0 deletions qt/model_view/updateableModel/UpdateableModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,11 @@ class UpdateableModel : public BaseModel
// reconstruct vector of changed roles
QVector<int> roles;
roles.reserve(m_changedRoles.count());
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
for (int role : std::as_const(m_changedRoles))
#else
for (int role : qAsConst(m_changedRoles))
#endif
{
roles.append(role);
}
Expand Down
4 changes: 4 additions & 0 deletions qt/tabWindow/src/tabwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ void TabWindowManager::removeWindow(TabWindow *window)

TabWindow *TabWindowManager::possibleWindow(TabWindow *currentWindow, QPoint globalPos)
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
for (auto tabWindow : std::as_const(m_windows))
#else
for (auto tabWindow : qAsConst(m_windows))
#endif
{
if (tabWindow == currentWindow)
continue;
Expand Down

0 comments on commit 7e7a98b

Please sign in to comment.