什么是QVariant,什么时候应该使用?

时间:2021-07-19 18:06:38

What is a QVariant and when should it be used?

什么是QVariant,什么时候应该使用?

2 个解决方案

#1


12  

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.

QVariant用于存储对您不一定知道内部内容的值的引用。这是一种创建API的方法,可以接受“任何东西”作为对未知类型的引用。 IE,而不是必须有一个接受long的API,另一个用于int,另一个用于float,另一个用于字符串,你可以拥有一个接受QVariant的API。

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.

然后在一个更复杂的函数里面,你需要某种类型(比如在QSettings中),你可以在获取它之后从Variant中提取你需要的东西。

#2


5  

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.

QVariant是变量的容器。它可以存储不同类型的变量。在某种程度上类似于无效*。但它为您提供有关存储类型的信息。

It can be used for example to return different types of values from a function.

例如,它可用于从函数返回不同类型的值。

#1


12  

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.

QVariant用于存储对您不一定知道内部内容的值的引用。这是一种创建API的方法,可以接受“任何东西”作为对未知类型的引用。 IE,而不是必须有一个接受long的API,另一个用于int,另一个用于float,另一个用于字符串,你可以拥有一个接受QVariant的API。

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.

然后在一个更复杂的函数里面,你需要某种类型(比如在QSettings中),你可以在获取它之后从Variant中提取你需要的东西。

#2


5  

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.

QVariant是变量的容器。它可以存储不同类型的变量。在某种程度上类似于无效*。但它为您提供有关存储类型的信息。

It can be used for example to return different types of values from a function.

例如,它可用于从函数返回不同类型的值。