diff --git a/src/corelib/tools/qset.h b/src/corelib/tools/qset.h index b75f19f242..7330b5e91c 100644 --- a/src/corelib/tools/qset.h +++ b/src/corelib/tools/qset.h @@ -228,10 +228,13 @@ Q_INLINE_TEMPLATE void QSet::reserve(qsizetype asize) { q_hash.reserve(asize) template Q_INLINE_TEMPLATE QSet &QSet::unite(const QSet &other) { - if (!q_hash.isSharedWith(other.q_hash)) { - for (const T &e : other) - insert(e); - } + if (q_hash.isSharedWith(other.q_hash)) + return *this; + QSet tmp = other; + if (size() < other.size()) + swap(tmp); + for (const auto &e : std::as_const(tmp)) + insert(e); return *this; } diff --git a/tests/auto/corelib/tools/qset/tst_qset.cpp b/tests/auto/corelib/tools/qset/tst_qset.cpp index 073004d279..cbe6230207 100644 --- a/tests/auto/corelib/tools/qset/tst_qset.cpp +++ b/tests/auto/corelib/tools/qset/tst_qset.cpp @@ -856,7 +856,7 @@ void tst_QSet::setOperationsOnEmptySet() empty.unite(nonEmpty); QCOMPARE(empty, nonEmpty); - QVERIFY(empty.isDetached()); + QVERIFY(!empty.isDetached()); } }