5

QPair is nice, but what if I need 3 items? Should I just make a struct, or does Qt have me covered?

Anon
  • 2,267
  • 3
  • 34
  • 51

3 Answers3

13

As QTBUG-22441 indicates, the developers have no intention of adding a Qt analog of std::tuple. (Even though QTBUG-3283 gives us hope that it could be done, it's dated Dec'09, while the newer report, with a WONTFIX, is dated May'15). Thus, you need to fall back to std::tuple or come up with your own data structure.

Quote from Marc Mutz:

A hypothetical QTuple wouldn't do anything differently, anyway, except drain Qt developer resources.

Moreover, the docs for Qt 5 Algorithms module state the following explicitly:

Historically, Qt used to provide functions which were direct equivalents of many STL algorithmic functions. Starting with Qt 5.0, you are instead encouraged to use directly the implementations available in the STL; most of the Qt ones have been deprecated (although they are still available to keep the old code compiling).

So using STL when programming with Qt 5 is officially encouraged, should it become a necessity.

iksemyonov
  • 4,106
  • 1
  • 22
  • 42
  • The quote and link only mentions STL algorithms, so what about containers? – Bernhard Sep 11 '18 at 12:18
  • 1
    @Bernhard See http://doc.qt.io/qt-5/containers.html. There is a key difference between Qt Containers and the STL ones: Qt implements implicit sharing and copy-on-write, whereas STL does not (both by design, I think). Now, when it comes to C++11, which for containers means move semantics, both STL and Qt5 containers implement that. Also, check out this answer: https://stackoverflow.com/a/1668549/342384 – iksemyonov Sep 14 '18 at 13:37
5

You can create your own structure using Qpair<Qpair<item1, item2>, item3>. Last time I used something like this to achieve what you say.

Note that for all the operations to work properly, you need to override them. The first item is a composed item (the item1+item2 pair).

Simply Me
  • 1,579
  • 11
  • 23
  • I was thinking of that, but its messy. Upvote but not accepted. – Anon Feb 20 '16 at 16:16
  • 1
    Actually, it depends. You simply need to override actions for `item1` and `item2` to ensure the comparation is made in a correct manner. I say this from my experiece with this. – Simply Me Feb 20 '16 at 16:22
  • 1
    @SimplyMe Even using a tuple is considered messy (as it's trying to hide the fact that a new type is needed). So your imitation of a tuple, which is an imitation of a type is indeed really messy :) – BartoszKP Feb 20 '16 at 16:35
  • 1
    I know, but it's a good workarround :). You can override 2-3 operators and it will actually act in a normal manner. – Simply Me Feb 20 '16 at 16:39
1

For some simpler cases, you can use a QVector<QVariant> or QList<QVariant>, provided you only use QVariant-supported data types.

An interesting note in the docs say that

QVariant can be extended to support other types than those mentioned in the Type enum. See Creating Custom Qt Types for details.

That second link includes how to create a QVariant-storable custom type. There's also the Custom Type Example.

Ronan Paixão
  • 8,297
  • 1
  • 31
  • 27