Better_Software_Header_MobileBetter_Software_Header_Web

Find what you need - explore our website and developer resources

On the Removal of toSet(), toList() and Others

or "How Do I Convert a QList to QSet in Qt 6?"

// Creates a QList from the [begin, end) range
QList<int> intList( begin, end );
QList<int> intList = ~~~;

// Creates a QSet from a QList
QSet<int> intSet( intList.begin(), intList.end() );
QList<int> intList = ~~~;

// Creates a QSet from a QList (in Qt 5)
QSet<int> intSet = intList.toSet();
QList<int> intList = getAnswers();

if (intList.contains(42))
   std::cout << "The Answer has been found!\n";
std::vector<int> intVector = getAnswers();

if (std::ranges::contains(intVector, 42)) // if P2302 gets adopted
   std::cout << "The Answer has been found!\n";
std::vector<int> intVector = ~~~;

if (std::find(intVector.begin(), intVector.end(), 42) != intVector.end())
   std::cout << "The Answer has been found!\n";
QList<int> list = ~~~;

// Remove duplicates, and get back to the original list
// For the love of kittens, DON'T DO THIS!
list = list.toSet().toList();
QList<int> list = ~~~;

// Process elements, skipping duplicates.
// This is OK-ish, but there are better solutions!
const QSet<int> set = list.toSet();
for (int i : set)
  process(i);
QSet<int> temp;
for (int i = 0; i < N; ++i) {
  temp += getList(i).toSet();
}
QList<int> result = temp.toList();
std::sort(container.begin(), container.end());
container.erase(std::unique(container.begin(), container.end()),
                container.end());
KDToolBox::DuplicateTracker<T> tracker;
const auto isDuplicate = [&tracker](const T &t) { return tracker.hasSeen(t); };
container.erase(std::remove_if(container.begin(), container.end(), isDuplicate),
                container.end());
KDToolBox::DuplicateTracker<T> tracker;
for (const T &t : container) {
  if (!tracker.hasSeen(t))
    process(t);
}
QSet<int> set(list.begin(), list.end());
QList<int> getList();

QSet<int> set(getList().begin(), getList().end()); // !!! WRONG !!!
QList<int> getList();

const QList<int> list = getList();
QSet<int> set(list.begin(), list.end()); // OK
QSet<int> set1 = list | ranges::to<QSet>();
QSet<int> set2 = getList() | ranges::to<QSet>();
QSet<int> set1 = list | kdToContainer<QSet>();
QSet<int> set2 = getList() | kdToContainer<QSet>();

About KDAB


17 Comments

23 - Jun - 2021

David Nolden

24 - Jun - 2021

Giuseppe D'Angelo

24 - Jun - 2021

David Nolden

25 - Jun - 2021

Giuseppe D'Angelo

23 - Jun - 2021

David Nolden

30 - Jun - 2021

Ilya

30 - Jun - 2021

André Somers

27 - Jul - 2021

Tired Today

27 - Jul - 2021

Giuseppe D'Angelo

2 - Aug - 2021

round earther

5 - Aug - 2021

Giuseppe D'Angelo

21 - Aug - 2021

Tristan Lewis

22 - Aug - 2021

Giuseppe D'Angelo

20 - Dec - 2021

Doug Rogers

template<typename T> QSet<T> fromList(QList<T> &list)
{
    QSet<T> set(list.begin(), list.end());
    return set;
}

template<typename T> QSet<T> toSet(QList<T> &list)
{
    QSet<T> set(list.begin(), list.end());
    return set;
}

20 - Dec - 2021

Giuseppe D'Angelo

QList<int> myList = ~~~;

auto set1 = myList | kdToContainer<QSet>();

13 - Oct - 2022

Pavel Celba

13 - Oct - 2022

Giuseppe D'Angelo

GiuseppeD'Angelo

Giuseppe D’Angelo

Senior Software Engineer

Related Content

Learn Modern C++

Learn more