Effective Qt: Understand the Qt Containers

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 a const_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 QFlags as Q_PRIMITIVE_TYPE if there’s a chance they will be held in Qt containers.
  • Declare your value-types Q_MOVABLE_TYPE if 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 (std or Q) over QList.
  • Avoid QList<T> where T is not declared as either Q_MOVABLE_TYPE or Q_PRIMITIVE_TYPE or where sizeof(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 which QList is not inefficient.

Hope you enjoy!


About marcmutz
Marc Mutz is a Senior Software Engineer, Trainer, and Consultant with KDAB.

3 Responses to Effective Qt: Understand the Qt Containers

  1. Kromain says:

    Prime knowledge from The Jedi Master himself 😉
    Awesome article, thanks for sharing!

  2. thorGT says:

    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.

    • marcmutz says:

      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 why Q_MOVABLE_TYPE is called has_trivial_relocate in EASTL.

      But no, there’s no such thing as has_trivial_relocate in C++11. There is is_trivially_copyable ([meta.unary.prop], [class] clause 6) which indicates that you can copy such classes with memcpy(), but that is much stronger than has_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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: