PropertyChanges Element Reference

The PropertyChanges element describes new property bindings or values for a state.

This element was introduced in Qt 4.7.

Detailed Description

The PropertyChanges element describes new property bindings or values for a state.

PropertyChanges is used to define the property values or bindings in a State. This enables an item's property values to be changed when it changes between states.

To create a PropertyChanges object, specify the target item whose properties are to be modified, and define the new property values or bindings. For example:

  1. import QtQuick 1.0
  2.  
  3. Item  {
  4.     id: container
  5.     width: 300; height: 300
  6.  
  7.     Rectangle  {
  8.         id: rect
  9.         width: 100; height: 100
  10.         color: "red"
  11.  
  12.         MouseArea  {
  13.            id: mouseArea
  14.            anchors.fill: parent
  15.         }
  16.  
  17.         states: State  {
  18.            name: "resized"; when: mouseArea.pressed
  19.            PropertyChanges  { target: rect; color: "blue"; height: container.height }
  20.         }
  21.     }
  22. }

When the mouse is pressed, the Rectangle changes to the resized state. In this state, the PropertyChanges object sets the rectangle's color to blue and the height value to that of container.height.

Note this automatically binds rect.height to container.height in the resized state. If a property binding should not be established, and the height should just be set to the value of container.height at the time of the state change, set the explicit property to true.

A PropertyChanges object can also override the default signal handler for an object to implement a signal handler specific to the new state:

  1. PropertyChanges  {
  2.     target: myMouseArea
  3.     onClicked: doSomethingDifferent()
  4. }

Note: PropertyChanges can be used to change anchor margins, but not other anchor values; use AnchorChanges for this instead. Similarly, to change an Item's parent value, use ParentChanges instead.

Resetting property values

The undefined value can be used to reset the property value for a state. In the following example, when theText changes to the widerText state, its width property is reset, giving the text its natural width and displaying the whole string on a single line.

  1.                 Rectangle  {
  2.     width: 300; height: 200
  3.  
  4.     Text  {
  5.         id: myText
  6.         width: 50
  7.         wrapMode: Text.WordWrap
  8.         text: "a text string that is longer than 50 pixels"
  9.  
  10.         states: State  {
  11.             name: "widerText"
  12.             PropertyChanges  { target: myText; width: undefined }
  13.         }
  14.     }
  15.  
  16.     MouseArea  {
  17.         anchors.fill: parent
  18.         onClicked: myText.state = "widerText"
  19.     }
  20. }

Immediate property changes in transitions

When Transitions are used to animate state changes, they animate properties from their values in the current state to those defined in the new state (as defined by PropertyChanges objects). However, it is sometimes desirable to set a property value immediately during a Transition, without animation; in these cases, the PropertyAction element can be used to force an immediate property change.

See the PropertyAction documentation for more details.

Properties

Property Documentation

If explicit is set to true, any potential bindings will be interpreted as once-off assignments that occur when the state is entered.

In the following example, the addition of explicit prevents myItem.width from being bound to parent.width. Instead, it is assigned the value of parent.width at the time of the state change.

  1.                 PropertyChanges  {
  2.     target: myItem
  3.     explicit: true
  4.     width: parent.width
  5. }

By default, explicit is false.

  • restoreEntryValues : bool

This property holds whether the previous values should be restored when leaving the state.

The default value is true. Setting this value to false creates a temporary state that has permanent effects on property values.

  • target : Object

This property holds the object which contains the properties to be changed.

Notes provided by the Qt Community

No notes