Maxence Robin
Maxence Robin

Reputation: 33

How does QVariant::fromValue work?

I was wondering how does the static method "fromValue" of QVariant works, here is the description:

Returns a QVariant containing a copy of value. Behaves exactly like setValue() otherwise.

How is it possible that this method has two different behavior depending on if it was called from an instance or from its static version? Because it's impossible to make a non-static method with the same prototype of another static method and I don't see how you can tell how it was called inside the method itself.

I'm interested in that because i was trying to make something similar for a fabric method.

Upvotes: 1

Views: 4043

Answers (1)

skypjack
skypjack

Reputation: 50550

QVariant::fromValue is defined as it follows:

template<typename T>
static inline QVariant fromValue(const T &value)
{ return qVariantFromValue(value); }

qVariantFromValue below:

template <typename T>
inline QVariant qVariantFromValue(const T &t)
{ return QVariant(qMetaTypeId<T>(), &t, QTypeInfo<T>::isPointer); }

That is exactly the same constructor used by setValue internally under certain circumstances (see the code for further details).

Because of that, I'd say that @Mat in the comments is almost right, you misunderstood the documentation.
You should rather read it as:

It behaves more or less like setValue, but for the fact that it returns a QVariant.

See the full code for further details.

Upvotes: 3

Related Questions