Reputation: 2492
QPair is nice, but what if I need 3 items? Should I just make a struct, or does Qt have me covered?
Upvotes: 7
Views: 11757
Reputation: 8665
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.
Upvotes: 1
Reputation: 4196
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.
Upvotes: 16
Reputation: 1587
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).
Upvotes: 5