Effective Qt: Understand the Qt Containers
2011-08-15 3 Comments
The third of my Effective Qt columns, “Understand the Qt Containers“, just went live on my site.
If you know me, you will know what awaits you there
I hope I have not disappointed you. That said, this is just a first part. I plan to eventually extend it to cover more containers (currently, just QVector and QList), and give more data and assembler code for the containers I’ve already described.
These are the guidelines this item covers:
- Remember that QList has nothing to do with std::list, and QSet has nothing to do with std::set.
- Be aware of the two Qt container APIs, Qt-ish and STL-compatible, and avoid mixing their use in the same project.
- Familiarise yourself with the STL containers and the additional features they offer.
- Prefer member-swap over assignment wherever possible to express move semantics. Use C++11 rvalue move semantics if the class and the compiler support them.
- [[EDIT 2011-08-25: new]] Always use
constBegin()/constEnd()(cbegin()/cend()) when assigning the result to aconst_iterator. - Avoid modifying a container (remove/insert elements) while iterating. If you have to, use an STL algorithm, such as one of the
std::remove()family. - Prefer the STL-compatible iterators over the Java-style ones.
- If you do use the Java-style iterators, avoid using the mutating ones.
- Prefer to use const references as the first argument of
Q_FOREACH, unless the element type is customarily passed by value. - Familiarise yourself with
BOOST_FOREACH, and the additional features it offers. - Declare your enums and
QFlagsasQ_PRIMITIVE_TYPEif there’s a chance they will be held in Qt containers. - Declare your value-types
Q_MOVABLE_TYPEif there’s a chance they will be held in Qt containers. - Don’t change the classification of a type if you need to maintain binary compatibility.
- Prefer
vector(stdorQ) overQList. - Avoid
QList<T>whereTis not declared as eitherQ_MOVABLE_TYPEorQ_PRIMITIVE_TYPEor wheresizeof(T) != sizeof(void*)(remember to check both 32 and 64-bit platforms). - [[EDIT 2011-08-16: new]] Avoid using vectors of types for which Qt APIs customarily use
QList, and for whichQListis not inefficient.
Hope you enjoy!

Prime knowledge from The Jedi Master himself
Awesome article, thanks for sharing!
Awesome!
Now that we have the new C++ Standard, “move semantics”, which is one of the main reasons why Qt Containers exist, is also available for the STL containers. Does this mean that Qt will move to STL in the (not so) near future? I see next to no point in duplication given that the main problem with STL containers (from Qt POV) is now fixed.
C++11 move semantics have nothing to do with
Q_MOVABLE_TYPE, and Qt containers don’t support them. Certainly not on a pre-C++11 compiler. It’s an unfortunate name collision, and the reason whyQ_MOVABLE_TYPEis calledhas_trivial_relocatein EASTL.But no, there’s no such thing as
has_trivial_relocatein C++11. There isis_trivially_copyable([meta.unary.prop], [class] clause 6) which indicates that you can copy such classes withmemcpy(), but that is much stronger thanhas_trivial_relocate, which only says you can relocate classes that way. In particular, all reference-counted classes are trivially relocatable, but most certainly not trivially copyable.I’ve updated the text, thanks.