このページでは

Behavior QML Type

プロパティの変更に対するデフォルトのアニメーションを定義します。詳細...

Import Statement: import QtQuick

プロパティ

詳細な説明

Behaviorは、特定のプロパティの値が変更された際に適用されるデフォルトのアニメーションを定義します。

たとえば、次の「Behavior」は、Rectangle のwidth の値が変更されるたびにNumberAnimation が実行されるように定義しています。MouseArea がクリックされると、width が変更され、この「Behavior」のアニメーションがトリガーされます:

import QtQuick

Rectangle {
    id: rect
    width: 100; height: 100
    color: "red"

    Behavior on width {
        NumberAnimation { duration: 1000 }
    }

    MouseArea {
        anchors.fill: parent
        onClicked: rect.width = 50
    }
}

なお、1つのプロパティに割り当てられるBehaviorは1つだけです。1つのBehavior内で複数のアニメーションを実装するには、ParallelAnimation またはSequentialAnimation を使用してください。

状態変更に、Behaviorと同じプロパティに対応するTransition が設定されている場合、その状態変更に関してはTransition のアニメーションがBehaviorよりも優先されます。状態変更のアニメーションにBehaviorを使用する際の一般的なアドバイスについては、「 Qt Quick のBehaviorを状態と併用する」を参照してください。

「 Qt Quick におけるアニメーションとトランジション」 、「 Behavior の例」、および Qt Qml。

プロパティのドキュメント

animation : Animation [default]

このプロパティは、ビヘイビアがトリガーされたときに実行されるアニメーションを保持します。

enabled : bool

このプロパティは、追跡対象のプロパティの値が変更された際に、その挙動がトリガーされるかどうかを指定します。

デフォルトでは、Behaviorは有効になっています。

targetProperty group

targetProperty.name : string [read-only, since QtQuick 2.15]

targetProperty.object : QtObject [read-only, since QtQuick 2.15]

プロパティ説明
名前このプロパティには、このBehaviorによって制御されているプロパティの名前が格納されます。
オブジェクトこのプロパティには、このBehaviorによって制御されているプロパティのオブジェクトが格納されます。

このプロパティを使用すると、制御対象のプロパティの名前またはオブジェクトに基づいて、カスタム・ビヘイビアを定義できます。

次の例は、制御対象のプロパティが変更されたときに、ターゲットオブジェクトをフェードアウトおよびフェードインさせるビヘイビアを定義しています:

// FadeBehavior.qml
import QtQuick 2.15

Behavior {
    id: root
    property Item fadeTarget: targetProperty.object
    SequentialAnimation {
        NumberAnimation {
            target: root.fadeTarget
            property: "opacity"
            to: 0
            easing.type: Easing.InQuad
        }
        PropertyAction { } // actually change the controlled property between the 2 other animations
        NumberAnimation {
            target: root.fadeTarget
            property: "opacity"
            to: 1
            easing.type: Easing.OutQuad
        }
    }
}

これを使用すると、テキストが変更された際にアニメーションを適用できます:

import QtQuick 2.15

Text {
    id: root
    property int counter
    text: counter
    FadeBehavior on text {}
    Timer {
        running: true
        repeat: true
        interval: 1000
        onTriggered: ++root.counter
    }
}

これらのプロパティは、QtQuick 2.15で導入されました。

targetValue : Variant [read-only, since QtQuick 2.13]

このプロパティには、Behaviorによって制御されているプロパティの目標値が格納されます。この値は、アニメーションが開始される前にBehaviorによって設定されます。

このプロパティは QtQuick 2.13 で導入されました。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.