QPair is nice, but what if I need 3 items? Should I just make a struct, or does Qt have me covered?
-
2Do you need a 'Q' structure or can you use `std::tuple`? – Thomas Matthews Feb 20 '16 at 16:05
-
I'd rather stick to Q, but that is a good suggestion too! – Akiva Feb 20 '16 at 16:05
-
1What's wrong with a struct? – dtech Feb 20 '16 at 16:39
-
It is just an extra step. – Akiva Feb 20 '16 at 16:40
-
1@Akiva - actually it will be shorter - a struct you can use directly everywhere, while the tuple you have to declare every time you want to use it, unless you typedef it, in which case it will be the same as the struct. – dtech Feb 20 '16 at 17:32
2 Answers
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.
![](../../users/profiles/342384.webp)
- 3,923
- 1
- 19
- 37
-
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
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).
![](../../users/profiles/5058218.webp)
- 1,519
- 9
- 22
-
-
1Actually, 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
-
1I 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