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

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

问题描述

考虑此项目结构:

MyComponent.qml:

Item {
    id: innerId
}

Usage.qml:

MyComponent {
    id: outerId
}

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

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.

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

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?

这将说明如何在MyComponent.qml中将对象称为innerId和在Usage.qml中将对象称为outerId,但在两个地方都具有相同的对象.

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.

推荐答案

id仅在该qml文件内部可见.该ID 不是属性,而是特殊属性.不要让语法欺骗您,它只是为了与qml的习惯用法保持一致,它看起来像是一个属性,但完全不同.

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.

id属性

The id Attribute

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

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.

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

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.

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

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.

id不是属性,并且只能从该文件内部进行访问,如果需要公开一些对象以使其从外部可见,则需要执行以下操作:

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
  }
}

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

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?

该ID用于引用当前qml文件中某些qml类型的实例.如果通过对象声明"表示实例,那么是的,它是正确的. IMO对象"是一个模棱两可的对象,因为一个对象可以是很多东西,一个对象可以是一个类型,一个实例,一个属性,一个函数,一个JS对象...在这方面,我认为每个QML对象类型具有恰好一个id属性",文档中的用词不正确.

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.

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

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

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

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.

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

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

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

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.

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

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.

此外,请注意,与id不同,您可以在很大程度上覆盖"函数和属性,并且其行为有点虚幻,如

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天全站免登陆