
Originally Posted by
eclarkso
I'm happy to go into that, but note above that this issue is independent of using that class(I should change the thread title to "Q_PROPERTY of type QMap").
Not really. Using QMap in Q_PROPERTY is easy, just use typedef or subclass to avoid using a comma inside the macro. That's not a problem.
QVariant is an obvious candidate,
How about just QString? It can store both integers and texts as well. That would leave you with QVariantMap (or even QMap<QString,int>) which is already supported.
but it can't be used as the key type for a QMap (no operator<) or QHash (no global qHash). My solution was subclassing QVariant and implementing one or both of the above functions,
You don't need to subclass QVariant to do that. Just implement the following function, it should work:
bool operator<(const QVariant &v1, const QVariant &v2);
To copy to clipboard, switch view to plain text mode
On the other hand I'm not sure how you would like to use this structure...
QMap<QVariant, QVariant> map;
map[1000] = "x";
map["1000"] = "x";
// which key is "less", the first or the second one?
QMap<QVariant, QVariant> map;
map[1000] = "x";
map["1000"] = "x";
// which key is "less", the first or the second one?
To copy to clipboard, switch view to plain text mode
which seems preferable to having WidgetForStrings and WidgetForInt with literally the only difference being a property/member variable QMap<QString,int> vs. QMap<int,int>.
There are templates, you know...
template <typename T> MyClass {
QMap<T, int> map;
};
template <typename T> MyClass {
QMap<T, int> map;
};
To copy to clipboard, switch view to plain text mode
Bookmarks