QML - 什么是 id 以及它是如何工作的? [英] QML - what is the id and how does it work?

查看:38
本文介绍了QML - 什么是 id 以及它是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个项目结构:

MyComponent.qml:

项目{标识:内部标识}

Usage.qml:

我的组件 {标识:外标识}

乍一看,这似乎创建了一个同时具有 2 个不同 id 的对象.但这是不可能的,如果 id 被视为一个属性.

在我看来,id 与其说是对象的属性,不如说是对象声明的属性.这是真的吗?

它将解释我如何在 MyComponent.qml 中将对象称为 innerId 并在 Usage.qml 中将对象称为 outerId 但在两个地方都必须是同一个对象.

解决方案

id 仅在该 qml 文件中可见.id 不是属性,而是一个特殊的属性.不要让语法欺骗你,它只是为了符合 qml 的习惯用法,它可能看起来像一个属性,但它完全不同.

<块引用>

id 属性

每个 QML 对象类型都只有一个 id 属性.这个属性是由语言本身提供,不能重新定义或覆盖通过任何 QML 对象类型.

可以为对象实例的 id 属性分配一个值,以允许该对象被其他对象识别和引用.此 id 必须以小写字母或下划线开头,并且不能包含除字母、数字和下划线以外的字符.

从技术上讲,两个 id 可能会引用同一个对象,但事实并非如此,innerId 引用了 MyComponent.qml 中的 Item 实例code>,outerId 引用 Usage.qml 中的 MyComponetn 实例.在实践中,如果您从 MyComponetnUsage 获得 console.log(id),您将获得相同的对象实例,因为 MyComponent{} 实例只是 MyComponent.qml 中该 Item 的另一个名称.

id 不是属性,只能从该文件内部访问,如果您需要将某些对象公开为从外部可见,则需要这样做:

Item {//Something.qml属性项目 innerItem :innerId物品 {标识:内部标识}}

<块引用>

在我看来,id 与其说是对象的属性,不如说是它是对象声明的属性.这是真的吗?

id 用于引用当前 qml 文件中某些 qml 类型的实例.如果对象声明"是指一个实例,那么是的,这是真的.IMO对象"是一个模棱两可的,因为一个对象可以是很多东西,一个对象可以是一个类型、一个实例、一个属性、一个函数、一个JS对象……在这方面我认为每个QML对象文档中的类型只有一个 id 属性"的措辞不正确.

id 只适用于 qml 类型的实例,属性和函数以不同的方式工作,并且可以从外部访问.

如果您需要对其用途进行类比,则 id 可能会被视为类似于私有类成员的东西 - 它仅在类型内部可见,如果您需要将其暴露给外部 - 您需要为它创建一个访问器.

<块引用>

这似乎创建了一个具有 2 个不同 id 的对象同时

这不是真的,因为您将无法使用 innerId 处理 MyComponent,所以不,它没有 2 个不同的 id,它没有没有任何个ID.id 并不真正属于该对象,它只是在当前源文件中与它相关联.如上所述,两个 id 将引用同一个对象,但该对象没有两个 id.

它的工作原理(我不是指实际的实现)类似于 C++ 中的引用.您可以在不同的地方使用不同的名称对同一个对象进行多次引用.在 qml 中,您不必像在 C++ 中那样为实例编写名称,但如果您想引用对象,通常的方法是使用 id,尽管根据对象树,您也可以使用 parent 属性.不建议在大型 qml 文件中过度使用 id,因为这可能会降低性能.

另外,请注意,与 id 不同,您可以非常覆盖"函数和属性,并且行为有点不确定,如 this question,例如,如果您使用字符串属性覆盖 int 属性,则该对象最终将拥有该属性两次,但如果您迭代该对象,你不会找到一个 int 和一个 string,而是找到两个 string.

Consider this project structure:

MyComponent.qml:

Item {
    id: innerId
}

Usage.qml:

MyComponent {
    id: outerId
}

At first glance it seems like this creates a single object that has 2 different id's simultaneously. But that's impossible, if id is to be considered a property.

To me it seems that an id is not so much a property of an object as it is a property of an object declaration. Is that true?

It would explain how I can refer to the object as innerId in MyComponent.qml and as outerId in Usage.qml yet have it be the same object in both places.

解决方案

The id is only visible inside that qml file. The id is not a property, but a special attribute. Don't let the syntax fool you, it was just intended to be in line with qml's idioms, it may look like a property but it is something different altogether.

The id Attribute

Every QML object type has exactly one id attribute. This attribute is provided by the language itself, and cannot be redefined or overridden by any QML object type.

A value may be assigned to the id attribute of an object instance to allow that object to be identified and referred to by other objects. This id must begin with a lower-case letter or an underscore, and cannot contain characters other than letters, numbers and underscores.

Technically, it may appear both ids reference the same object, but that's not the case, the innerId references the Item instance in MyComponent.qml, and the outerId references the MyComponetn instance in Usage.qml. In practice, if you console.log(id) from MyComponetn and Usage you will get the same object instance, since the MyComponent {} instance is just another name for that Item from MyComponent.qml.

The id is not a property, and it can only be access from inside that file, if you need to expose some object to be visible from the outside, you need to do this:

Item { // Something.qml
  property Item innerItem : innerId
  Item {
    id: innerId
  }
}

To me it seems that an id is not so much a property of an object as it is a property of an object declaration. Is that true?

The id is used to refer to an instance of some qml type in the current qml file. If by "object declaration" you mean an instance, then yes, it is true. IMO "object" is a ambiguous, as an object can be a lot of things, an object can be a type, an instance, a property, a function, a JS object... In this regard I think the "Every QML object type has exactly one id attribute" from the documentation is not worded properly.

The id only applies to qml type instances, properties and functions work in a different way, and are accessible from the outside.

If you need to make an analogy to what it's used for, an id might be seen as something similar to a private class member - it is only visible inside the type, and if you need to expose it to the outside - you need to make an accessor for it.

it seems like this creates a single object that has 2 different id's simultaneously

This is not true, as you won't be able to address MyComponent with innerId, so no, it doesn't have 2 different ids, it doesn't have any ids. The id does not really belong to the object, it is just associated to it in the current source file. As mentioned above, the two ids will be referring to the same object, but the object doesn't have two ids.

An analogy to how it works (and I don't mean the actual implementation) would be something similar to references in C++. You can have multiple references to the same object, in different places and with different names. In qml you don't have to write names for instances like in C++, but if you want to refer to an object the usual approach is to use an id, although depending on the object tree you can use the parent property as well. It is not recommended to go overboard with ids in big qml files, as that could degrade performance.

Also, note that unlike ids, you can very much "override" functions and properties, and the behavior is kind of iffy as elaborated in this question, for example, if you override an int property with a string property, the object will end up having that property twice, but if you iterate the object, you will not find one int and one string, but the string twice.

这篇关于QML - 什么是 id 以及它是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆