What is a QVariant
and when should it be used?
2 Answers
QVariant is used to store references to values where you don't necessarily know what is inside. It's a way to create APIs that can accept "anything" as a reference to an unknown type. IE, instead of having to have an API that accepts a long, and another for an int, and another for a float, and another for a string you can have a single API that accepts a QVariant instead.
Then inside a more complex function where you need a certain type (like in QSettings) you can extract just what you need from the Variant after getting it back.

- 21,735
- 2
- 38
- 69
QVariant is a container of variables. It can store variables of different types. Similar in some way to void*. But it provides You information about the stored type.
It can be used for example to return different types of values from a function.

- 2,470
- 2
- 30
- 54
-
I would say it's like a container for a void* to a value you are interested in. It provides some extra functionality that helps you convert the contained thing back to it's original type (for example, you can ask it for the Type of the object, and if it's the type you want (for example, QString), then you can have QVariant convert its contained value back to a QString for you). – michaelmoo May 02 '14 at 16:14