Details
-
Suggestion
-
Resolution: Done
-
Not Evaluated
-
None
-
None
Description
QVariant can hold a value of (almost) any type, given the requirements are met, but there is no easy, universal way of creating a QVariant of any value.
The value is either basic type, and Qt has a easy, non-explicit c-tor for it.
or
The value is of custom type and the user must use the clunky and verbose QVariant::fromValue().
My suggestion is to add a simple function "qVar()" to be able to easily create a qvariant from any value with a simple and concise syntax.
Usage:
auto var = isCustom ? qVar(getCustom()) : qVar(getStandard());
Usage:
QVariant getValue(Data data, Type type) { switch(type) { case type_int: return data.someIntValue(); case type_string: return data.someStringValue(); case type_custom: return qVar(data.someCustomValue()); } }
The implementation is trivial:
template<class T> QVariant qVar(T&& val) { return QVariant::fromValue(std::forward<T>(val)); }
One interesting side effect is that the user can override the function for his own type:
QVariant qVar(const Custom& c) { // magic }