C ++中QVariant的等价物是什么?

时间:2021-08-13 16:50:01

I am trying to port a Qt application to C++ using STL. What is the equivalent of QVariant in C++? QVariant can store any data type - a container that holds heterogenous - different types of objects. However, I have to port this application to C++. What is the equivalent of QVariant in C++?

我试图使用STL将Qt应用程序移植到C ++。 C ++中QVariant的等价物是什么? QVariant可以存储任何数据类型 - 一个容纳异类的容器 - 不同类型的对象。但是,我必须将此应用程序移植到C ++。 C ++中QVariant的等价物是什么?

2 个解决方案

#1


13  

What is the equivalent of QVariant in C++?

C ++中QVariant的等价物是什么?

The equivalent in C++ is called QVariant.

C ++中的等价物称为QVariant。

Semi-joke aside, it is probably the closest to union, but QVariant is so much more than that; meta types, CoW, etc.

除了半开玩笑,它可能是最接近联盟的,但QVariant远不止于此;元类型,CoW等

Actually, sharing implicitly is forbidden these days in STL, so that is another reason why you would not find anything like this off-hand.

实际上,这些天在STL中被禁止隐藏,所以这也是你不能找到这样的东西的另一个原因。

I would suggest to make notes to yourself what functionality exactly you need from a QVariant and make a judgement call whether it is actually worth dropping QtCore.

我建议你自己做一些QVariant所需的功能,然后判断它是否真的值得掉QtCore。

This is just a friendly reminder from the documentation:

这只是文档中的一个友好提醒:

The QVariant class acts like a union for the most common Qt data types.

QVariant类就像最常见的Qt数据类型的联合一样。

Because C++ forbids unions from including types that have non-default constructors or destructors, most interesting Qt classes cannot be used in unions. Without QVariant, this would be a problem for QObject::property() and for database work, etc.

因为C ++禁止联合包含具有非默认构造函数或析构函数的类型,所以大多数有趣的Qt类不能在联合中使用。如果没有QVariant,这对于QObject :: property()和数据库工作等都是一个问题。

However, since C++11 types with constructors and destructors are allowed. You have another question in here to ask from yourself:

但是,由于允许使用带有构造函数和析构函数的C ++ 11类型。你在这里有另一个问题要问自己:

Do I want to support everything that Qt does or am I happy to require at least C++11?

我是否想支持Qt所做的一切,或者我是否乐意至少要求C ++ 11?

To be fair, you could also look into the following boost variants below, but then you end up using boost instead of QtCore after the porting.

公平地说,您还可以查看下面的以下提升变体,但最终在移植后最终使用boost而不是QtCore。

This is another judgement call for yourself. They are not replacements for each other, neither technically, nor compatibility wise.

这是对自己的另一种判断。它们不是技术上的替代品,也不是兼容性的替代品。

Boost in this case is a build time dependency, while QtCore is a runtime. Qt would guarantee binary (and source) compatibility during the life cycle of the same major version, while boost may not do the same for such a long period as QtCore does.

在这种情况下,Boost是构建时依赖性,而QtCore是运行时。 Qt将保证在相同主要版本的生命周期内的二进制(和源)兼容性,而在QtCore这么长的时间内,boost可能不会做同样的事情。

Either way, none of these options are pure STL solutions in the end of the day the way I think you wanted it to.

无论哪种方式,这些选项都不是我认为你想要它的方式在一天结束时纯粹的STL解决方案。

#2


3  

Starting in C++17, there is std::any (may contain a value of any type) and std::variant (may contain a value of any of the given types). These will be supported out of the box in GCC 7, Clang 4.0 and Visual C++ 2017.

从C ++ 17开始,有std :: any(可能包含任何类型的值)和std :: variant(可能包含任何给定类型的值)。这些将在GCC 7,Clang 4.0和Visual C ++ 2017中开箱即用。

Neither of these is entirely equivalent (for example they don't support converting the internal value), but they will allow you to do most of what QVariant does.

这些都不是完全等价的(例如它们不支持转换内部值),但它们将允许您完成QVariant所做的大部分工作。

#1


13  

What is the equivalent of QVariant in C++?

C ++中QVariant的等价物是什么?

The equivalent in C++ is called QVariant.

C ++中的等价物称为QVariant。

Semi-joke aside, it is probably the closest to union, but QVariant is so much more than that; meta types, CoW, etc.

除了半开玩笑,它可能是最接近联盟的,但QVariant远不止于此;元类型,CoW等

Actually, sharing implicitly is forbidden these days in STL, so that is another reason why you would not find anything like this off-hand.

实际上,这些天在STL中被禁止隐藏,所以这也是你不能找到这样的东西的另一个原因。

I would suggest to make notes to yourself what functionality exactly you need from a QVariant and make a judgement call whether it is actually worth dropping QtCore.

我建议你自己做一些QVariant所需的功能,然后判断它是否真的值得掉QtCore。

This is just a friendly reminder from the documentation:

这只是文档中的一个友好提醒:

The QVariant class acts like a union for the most common Qt data types.

QVariant类就像最常见的Qt数据类型的联合一样。

Because C++ forbids unions from including types that have non-default constructors or destructors, most interesting Qt classes cannot be used in unions. Without QVariant, this would be a problem for QObject::property() and for database work, etc.

因为C ++禁止联合包含具有非默认构造函数或析构函数的类型,所以大多数有趣的Qt类不能在联合中使用。如果没有QVariant,这对于QObject :: property()和数据库工作等都是一个问题。

However, since C++11 types with constructors and destructors are allowed. You have another question in here to ask from yourself:

但是,由于允许使用带有构造函数和析构函数的C ++ 11类型。你在这里有另一个问题要问自己:

Do I want to support everything that Qt does or am I happy to require at least C++11?

我是否想支持Qt所做的一切,或者我是否乐意至少要求C ++ 11?

To be fair, you could also look into the following boost variants below, but then you end up using boost instead of QtCore after the porting.

公平地说,您还可以查看下面的以下提升变体,但最终在移植后最终使用boost而不是QtCore。

This is another judgement call for yourself. They are not replacements for each other, neither technically, nor compatibility wise.

这是对自己的另一种判断。它们不是技术上的替代品,也不是兼容性的替代品。

Boost in this case is a build time dependency, while QtCore is a runtime. Qt would guarantee binary (and source) compatibility during the life cycle of the same major version, while boost may not do the same for such a long period as QtCore does.

在这种情况下,Boost是构建时依赖性,而QtCore是运行时。 Qt将保证在相同主要版本的生命周期内的二进制(和源)兼容性,而在QtCore这么长的时间内,boost可能不会做同样的事情。

Either way, none of these options are pure STL solutions in the end of the day the way I think you wanted it to.

无论哪种方式,这些选项都不是我认为你想要它的方式在一天结束时纯粹的STL解决方案。

#2


3  

Starting in C++17, there is std::any (may contain a value of any type) and std::variant (may contain a value of any of the given types). These will be supported out of the box in GCC 7, Clang 4.0 and Visual C++ 2017.

从C ++ 17开始,有std :: any(可能包含任何类型的值)和std :: variant(可能包含任何给定类型的值)。这些将在GCC 7,Clang 4.0和Visual C ++ 2017中开箱即用。

Neither of these is entirely equivalent (for example they don't support converting the internal value), but they will allow you to do most of what QVariant does.

这些都不是完全等价的(例如它们不支持转换内部值),但它们将允许您完成QVariant所做的大部分工作。