首页 > 解决方案 > 在 QML 中的信号处理程序中使用转换属性时,“预期的标记 ','”

问题描述

我想Rectangle在鼠标悬停的情况下为 a 进行转换。我按照本教程创建了一个MouseArea这样的:

MouseArea{
        anchors.fill: parent
        hoverEnabled: true
        onEntered: {
            rMT.color = 'red'                    // rMT is my rectangle's id (rectangle_mouse_tracking)
            transitions: Transition {            // Expected token ','
                NumberAnimation {
                    property : "width"
                    easing.type: Easing.InOutQuad
                    duration : 200
                }
            }
        }
    ...
    ...

*还有其他可能的方法来制作动画,但我想知道上面的代码有什么问题?

目前我是 QML 的菜鸟。如果这个问题有点傻,很抱歉。

谢谢。

标签: qmlsyntax-error

解决方案


当属性改变时,在转换中定义的动画被执行。

如果您希望在width更改时执行动画,请更改 onEntered 中的宽度并执行 Rectangle 中定义的过渡。

语法错误是因为transitions必须在 Rectange{} 而不是 onEntered {}

import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick 2.0
Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    Rectangle {
        id: rMT
        width: 400
        height:400
        x:0
        color: 'blue'
        border.color: 'black'
        MouseArea{
            anchors.fill: parent
            hoverEnabled: true
            onEntered: {
                rMT.color = 'red'
                rMT.width = 100
            }
        }
        transitions: Transition{
            NumberAnimation {
                property : "width"
                from: 400
                easing.type: Easing.InOutQuad
                duration : 2000
            }

        }

    }
}

推荐阅读