使用附加属性、信号的正确方法是什么?
What's the rightway to use attached properties,signals?
我发现一些代码以一种奇怪的方式使用 Attached Properties,
。
案例 1
Flickable {
// ...
ScrollIndicator.vertical: ScrollIndicator { }
}
案例 2
ListView {
model: 3
delegate: Rectangle {
color: ListView.isCurrentItem ? "red" : "yellow"
}
}
案例 3 或案例 4...n
Item {
Component.onCompleted: {
console.log("hello")
}
}
我对 Attached Signals,Properties
感到困惑,不知道什么时候 Attached Signals,Properties
可以在我的代码中的某处使用(调用)并且不知道它的真正含义和实际用法?
docs说的很清楚:
Attached properties and attached signal handlers are mechanisms that
enable objects to be annotated with extra properties or signal
handlers that are otherwise unavailable to the object. In particular,
they allow objects to access properties or signals that are
specifically relevant to the individual object.
A QML type implementation may choose to create an attaching type in
C++ with particular properties and signals. Instances of this type can
then be created and attached to specific objects at run time, allowing
those objects to access the properties and signals of the attaching
type. These are accessed by prefixing the properties and respective
signal handlers with the name of the attaching type.
References to attached properties and handlers take the following
syntax form:
<AttachingType>.<propertyName>
<AttachingType>.on<SignalName>
简而言之,它允许在不修改基础的情况下添加新功能 class。
在第一个示例中,观察到添加了一个 ScrollIndicator(不一定是 Flickable 的一部分)而没有修改 Flickable 的行为。
在第二种情况下,将不是 ListView 对象的“ListView”属性 添加到每个委托,以便它可以访问有关视图的信息。应该记住,委托有它自己的范围,它不同于视图,例如你不能在它之外访问委托的属性。
在第三种情况下,添加了“Completed”对象,它具有在基础对象(Item)完成构建时发出的完成信号。
我发现一些代码以一种奇怪的方式使用 Attached Properties,
。
案例 1
Flickable {
// ...
ScrollIndicator.vertical: ScrollIndicator { }
}
案例 2
ListView {
model: 3
delegate: Rectangle {
color: ListView.isCurrentItem ? "red" : "yellow"
}
}
案例 3 或案例 4...n
Item {
Component.onCompleted: {
console.log("hello")
}
}
我对 Attached Signals,Properties
感到困惑,不知道什么时候 Attached Signals,Properties
可以在我的代码中的某处使用(调用)并且不知道它的真正含义和实际用法?
docs说的很清楚:
Attached properties and attached signal handlers are mechanisms that enable objects to be annotated with extra properties or signal handlers that are otherwise unavailable to the object. In particular, they allow objects to access properties or signals that are specifically relevant to the individual object.
A QML type implementation may choose to create an attaching type in C++ with particular properties and signals. Instances of this type can then be created and attached to specific objects at run time, allowing those objects to access the properties and signals of the attaching type. These are accessed by prefixing the properties and respective signal handlers with the name of the attaching type.
References to attached properties and handlers take the following syntax form:
<AttachingType>.<propertyName> <AttachingType>.on<SignalName>
简而言之,它允许在不修改基础的情况下添加新功能 class。
在第一个示例中,观察到添加了一个 ScrollIndicator(不一定是 Flickable 的一部分)而没有修改 Flickable 的行为。
在第二种情况下,将不是 ListView 对象的“ListView”属性 添加到每个委托,以便它可以访问有关视图的信息。应该记住,委托有它自己的范围,它不同于视图,例如你不能在它之外访问委托的属性。
在第三种情况下,添加了“Completed”对象,它具有在基础对象(Item)完成构建时发出的完成信号。