Is there a container who can store different types (yes, I really need to work with different kind of types) in Qt? I must create a new class to do this? If so, could you give me a hint to create it?
Asked
Active
Viewed 164 times
0
-
That would be QVariant. – user2672165 Jul 12 '14 at 14:37
-
Can I set a QVariant as a QMap? – user3713179 Jul 12 '14 at 17:16
-
Yes you can store `QMap
`or even user defined types, see http://stackoverflow.com/questions/3193275/how-to-verify-qvariant-of-type-qvariantusertype-is-expected-type – user2672165 Jul 12 '14 at 17:26
1 Answers
1
Most of containers in Qt are template based, then you can use them for different static types.
Another option is to use use
QVariant
, for example:QVector<QVariant> vec;
.A more dynamic solution is to use polymorphism, you can store pointers to a base class and so on.. .
PS: As a general rule, you should avoid this patterns. From Effective C++, by Scott Meyers:
Anytime you find yourself writing code of the form "if the object is of type T1, then do something, but if it's of type T2, then do something else," slap yourself.

masoud
- 55,379
- 16
- 141
- 208