7

QVariant似乎接受QList<QVariant>,而QVector<QVariant>不是QLinkedList<QVariant>。仅仅是因为它看到了QListQVector并且QLinkedList是基本相似(在抽象意义上)的数据结构吗?

我正在添加和std::vector到一个QVariant. 如果只使用 Qt API 而不是手动转换,则需要两次转换:

  1. std::vectorQVector
  2. QVectorQList

PS:我知道我可以直接添加std::vector这个,但我相信在这种情况下它不会知道它是对象的向量。QVariant

4

4 回答 4

12

在调用 qRegisterMetaType 函数后,您可以将所有内容存储在 QVariant 中。

因此,如果您调用qRegisterMetaType<std::vector<SomeObject> >("std::vector<SomeObject>");QVariant 将存储 std::vector。从它执行函数中读取这些值T QVariant::value () const,用于编写使用函数void QVariant::setValue ( const T & value )

PS:我知道我可以直接将 std::vector 添加到 QVariant ,但我相信在这种情况下它不会知道它是一个对象向量。

当您将类型注册到 QVariant 时,它会在操作该类型的项目时调用它的默认构造函数、复制构造函数和析构函数。所以将它与类和对象一起使用是没有害处的。

于 2011-05-30T15:20:13.493 回答
7

Simply because QList is by far the most commonly used container type, and adding overloads for all the others would make the QVariant interface even more complex than it already is. In any case, your problem seems to be not that QVariant doesn't support QVector (it does with a little work) but that QJson doesn't. I doubt that an extra call to QVector::toList() would cause a significant performance overhead.

于 2011-05-30T20:18:52.990 回答
2

对于像这样的模板类,您必须将每个特定的实例分别注册到元系统中。显然,巨魔们觉得有必要使用QList<QVariant>,但其他人都没有,所以他们只注册了一个。没有具体原因您不能自己注册它们。

于 2011-05-30T16:01:51.897 回答
0

我不是 100% 确定 QVariant 的实现,但我相信 QVariant 的大小直到运行时才确定。这意味着如果您尝试编写 QVector<QVariant>,编译器不知道要分配多少空间,因此会报告错误。LinkedList 也是如此。QList 之所以有效,是因为它的实现严格依赖于指针。

我打赌你会发现 QVector< QVariant* > 编译得很好。

大警告:我不是 Qt 专家,所以我可能会对此有所了解。但希望至少这能让你朝着正确的方向思考。

于 2011-05-30T15:19:45.207 回答