Are “var” and “variant” the same thing?

大憨熊 提交于 2019-12-05 01:28:05
Tim Meyer

According to the Qt 5.0 variant documentation:

The variant type is a generic property type. It is obsolete and exists only to support old applications; new applications should use var type properties instead.

So yes, it is the same, but you should always stick to var (unless you got an earlier version which does not support that yet).

This is not a completely new answer, but contains additional information about the answer provided by @Tim Meyer, based on my own experience:

  1. With Qt 4.* or QtQuick 1.*, property variant has to be used otherwise QML parsing errors will be produced.
  2. With Qt 5 or QtQuick 2.*, either property variant or property var can be used. But the latter one is recommended, as the former one is being deprecated.
  3. Qt 4 property variant or Qt 5 property var may be used for QML array or list declaration/definition. But if the type and unchangeable content of myArray are known in advance, property list<Type> can also be used. For example:

    property list<Item> myArray: [ Item {}, Item {} ]

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!