Attached Properties

Attached Properties

Certain objects provide additional properties by attaching properties to other objects. For example, the Keys element have properties that can attach to other QML objects to provide keyboard handling.

 Component {
     id: listdelegate
     Text {
         text: "Hello"
         color: ListView.isCurrentItem ? "red" : "blue"
     }
 }
 ListView {
     delegate: listdelegate
 }

The element ListView provides the delegate, listdelegate, the property isCurrentItem as an attached property. TheListView.isCurrentItem attached property provides highlight information to the delegate. Effectively, the ListView element attaches the ListView.isCurrentItem property to each delegate it creates.

 

 

Attached Signal Handlers

Attached signal handlers are similar to attached properties in that they attach to objects to provide additional functionality to objects. Two prominent elements, Component and Keys element provide signal handlers as attached signal handlers.

 Item {
     Keys.onPressed: console.log("Key Press Detected")
     Component.onCompleted: console.log("Completed initialization")
 }

Read the QML Signal and Handler Event System and the Keyboard Focus in QML articles for more information.

你可能感兴趣的:(properties)