QVariant 仅接受 QList 而不是 QVector 或 QLinkedList 是否有原因

发布于 2024-11-10 08:45:05 字数 705 浏览 0 评论 0 原文

QVariant 似乎接受 QList 并且不是QVector 也不是QLinkedList。仅仅是因为它将 QList、QVector 和 QLinkedList 视为本质上相似(抽象意义上)的数据结构吗?

我将 std::vector 添加到 QVariant 中。如果仅使用 Qt API 而不是手动转换,则需要两次转换:

  1. std::vectorQVector
  2. QVectorQList

PS:我知道我可以使用 this 但我相信在这种情况下它不会知道它是对象向量。

QVariant appears to accept QList<QVariant> and not QVector<QVariant> nor QLinkedList<QVariant>. Is it simply because it sees QList, QVector and QLinkedList as fundamentally similar (in an abstract sense) data structures?

I'm adding and std::vector to a QVariant. If using only the Qt API and not a manual conversion, this requires two conversions:

  1. From std::vector to QVector
  2. From QVector to QList

PS: I'm aware that I can add std::vector to QVariant directly with this but I believe in that case it won't know that it's a vector of objects.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

临走之时 2024-11-17 08:45:05

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

所以如果你调用 qRegisterMetaType >("std::vector"); QVariant 将存储 std::vector。
执行函数 T QVariant::value () const 从中读取这些值,写入时使用函数 void QVariant::setValue ( const T & value )

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

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

you may store everything in QVariant, after calling to qRegisterMetaType function.

so if you call qRegisterMetaType<std::vector<SomeObject> >("std::vector<SomeObject>"); QVariant WOULD store std::vector.
reading such values from it performing function T QVariant::value () const , for writing use function void QVariant::setValue ( const T & value )

PS: I'm aware that I can add std::vector to QVariant directly with this but I believe in that case it won't know that it's a vector of objects.

When you registering type to QVariant, it calls it's default constructor,copy constructor and destructor while manipulating items of that type. So there is no harm to use it with classes and objects.

月亮邮递员 2024-11-17 08:45:05

仅仅因为 QList 是迄今为止最常用的容器类型,为所有其他容器类型添加重载将使 QVariant 接口比现在更加复杂。无论如何,您的问题似乎不是 QVariant 不支持 QVector (它需要做一些工作),而是 QJson 不支持。我怀疑对 QVector::toList() 的额外调用会导致显着的性能开销。

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.

好听的两个字的网名 2024-11-17 08:45:05

对于像这样的模板化类,您必须向元系统单独注册每个特定的实例。显然,巨魔们觉得有必要使用 QList,但没有其他的,所以他们只注册了一个。没有具体原因您无法自行注册它们。

For templated classes like these, you have to register each specific instantiation with the meta system individually. Apparently, the Trolls felt the need to use QList<QVariant>, but none of the others, so they only registered the one. There's no specific reason you couldn't register them yourself.

夏至、离别 2024-11-17 08:45:05

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

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

重要警告:我不是 Qt 专家,所以我可能不同意这一点。但希望至少这能让你朝着正确的方向思考。

I'm not 100% sure of the implementation of QVariant, but I believe that the size of a QVariant is not determined until run time. This means that if you try to write QVector< QVariant > the compiler doesn't know how much space to allocate, so it reports an error. The same is true for LinkedList. QList works because it's implementation relies strictly on pointers.

I bet you would find that QVector< QVariant* > compiles just fine.

Big caveat: I'm not a Qt expert, so I might be off on this. But hopefully at the very least this gets you thinking in the right direction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文