Search code examples
c++qtqhash

How do you define a QHash with heterogeneous value types?


I need to have a QHash container that takes quint8 keys but takes heterogeneous types as values, all of which will be Qt containers or classes. As an example I might want to insert a QDate or QTime object or even quint8 as the value.

How can I define a type like this so I can use it in my other classes and fill appropriately at run time? I want to be able to access it as a global type. Is it possible?

N.B. Question has been edited to better reflect the OP's intent. Answers written before the edit are appropriate to the original question.


Solution

  • QVariant is a type which can store any of a wide range of value types, determined at runtime, so a QHash<quint8, QVariant> is what you want.

    See also https://en.wikipedia.org/wiki/Tagged_union for the general pattern.