QSet Class

The QSet class is a template class that provides a hash-table-based set. More...

Header: #include <QSet>
qmake: QT += core

Note: All functions in this class are reentrant.

Public Types

class const_iterator
class iterator
typedef ConstIterator
typedef Iterator
typedef const_pointer
typedef const_reference
typedef const_reverse_iterator
typedef difference_type
typedef key_type
typedef pointer
typedef reference
typedef reverse_iterator
typedef size_type
typedef value_type

Detailed Description

QSet<T> is one of Qt's generic container classes. It stores values in an unspecified order and provides very fast lookup of the values. Internally, QSet<T> is implemented as a QHash.

Here's an example QSet with QString values:


  QSet<QString> set;

To insert a value into the set, use insert():


  set.insert("one");
  set.insert("three");
  set.insert("seven");

Another way to insert items into the set is to use operator<<():


  set << "twelve" << "fifteen" << "nineteen";

To test whether an item belongs to the set or not, use contains():


  if (!set.contains("ninety-nine"))
      ...

If you want to navigate through all the values stored in a QSet, you can use an iterator. QSet supports both Java-style iterators (QSetIterator and QMutableSetIterator) and STL-style iterators (QSet::iterator and QSet::const_iterator). Here's how to iterate over a QSet<QWidget *> using a Java-style iterator:


  QSetIterator<QWidget *> i(set);
  while (i.hasNext())
      qDebug() << i.next();

Here's the same code, but using an STL-style iterator:


  QSet<QWidget *>::const_iterator i = set.constBegin();
  while (i != set.constEnd()) {
      qDebug() << *i;
      ++i;
  }

QSet is unordered, so an iterator's sequence cannot be assumed to be predictable. If ordering by key is required, use a QMap.

To navigate through a QSet, you can also use foreach:


  QSet<QString> set;
  ...
  foreach (const QString &value, set)
      qDebug() << value;

Items can be removed from the set using remove(). There is also a clear() function that removes all items.

QSet's value data type must be an assignable data type. You cannot, for example, store a QWidget as a value; instead, store a QWidget *. In addition, the type must provide operator==(), and there must also be a global qHash() function that returns a hash value for an argument of the key's type. See the QHash documentation for a list of types supported by qHash().

Internally, QSet uses a hash table to perform lookups. The hash table automatically grows and shrinks to provide fast lookups without wasting memory. You can still control the size of the hash table by calling reserve(), if you already know approximately how many elements the QSet will contain, but this isn't necessary to obtain good performance. You can also call capacity() to retrieve the hash table's size.

See also QSetIterator, QMutableSetIterator, QHash, and QMap.

Member Type Documentation

typedef QSet::ConstIterator

Qt-style synonym for QSet::const_iterator.

typedef QSet::Iterator

Qt-style synonym for QSet::iterator.

This typedef was introduced in Qt 4.2.

typedef QSet::const_pointer

Typedef for const T *. Provided for STL compatibility.

typedef QSet::const_reference

Typedef for const T &. Provided for STL compatibility.

typedef QSet::const_reverse_iterator

The QSet::const_reverse_iterator typedef provides an STL-style const reverse iterator for QSet.

It is simply a typedef for std::reverse_iterator<QSet::const_iterator>.

Warning: Iterators on implicitly shared containers do not work exactly like STL-iterators. You should avoid copying a container while iterators are active on that container. For more information, read Implicit sharing iterator problem.

This typedef was introduced in Qt 5.6.

See also QSet::rbegin(), QSet::rend(), QSet::reverse_iterator, and QSet::const_iterator.

typedef QSet::difference_type

Typedef for const ptrdiff_t. Provided for STL compatibility.

typedef QSet::key_type

Typedef for T. Provided for STL compatibility.

typedef QSet::pointer

Typedef for T *. Provided for STL compatibility.

typedef QSet::reference

Typedef for T &. Provided for STL compatibility.

typedef QSet::reverse_iterator

The QSet::reverse_iterator typedef provides an STL-style non-const reverse iterator for QSet.

It is simply a typedef for std::reverse_iterator<QSet::iterator>.

Warning: Iterators on implicitly shared containers do not work exactly like STL-iterators. You should avoid copying a container while iterators are active on that container. For more information, read Implicit sharing iterator problem.

This typedef was introduced in Qt 5.6.

See also QSet::rbegin(), QSet::rend(), QSet::const_reverse_iterator, and QSet::iterator.

typedef QSet::size_type

Typedef for int. Provided for STL compatibility.

typedef QSet::value_type

Typedef for T. Provided for STL compatibility.