Javascript TextField悬停高亮显示动画QML QtQuick.Control 2.15

Javascript TextField悬停高亮显示动画QML QtQuick.Control 2.15,javascript,qt,qml,Javascript,Qt,Qml,我有一个文本字段,我试图在悬停时突出显示它的背景。我正在使用一个属性动画,但它实际上并不做动画: import QtQuick 2.15 import QtQuick.Controls 2.15 import QtGraphicalEffects 1.15 TextField { id: txtfield height: 40 width: 250 QtObject{ id: internal // property var

我有一个文本字段,我试图在悬停时突出显示它的背景。我正在使用一个
属性动画
,但它实际上并不做动画:

import QtQuick 2.15
import QtQuick.Controls 2.15
import QtGraphicalEffects 1.15

TextField {
    id: txtfield

    height: 40
    width: 250

    QtObject{
        id: internal

//        property var dynamicWidth: {if(txtfield.hovered){
//                                   console.log('Damn')
//                                   bg.border.width = 1.5
//                               }else{
//                                   bg.border.width = 0
//                               }
//        }
    }
    color: '#ffffff'
    placeholderTextColor: "#7fffffff"
    selectedTextColor: '#000000'
    selectionColor: '#ffffff'
    font.family: "Verdana"
    font.pointSize: 10
    placeholderText: qsTr('Enter full name...')
    hoverEnabled: true

    background: Rectangle{
        id: bg
        color: "#2c313c"
        border.color: "#aa0000"
//        border.width: internal.dynamicWidth
        radius: 10

        PropertyAnimation{
            id: widthAnimation
            target: bg // rectangle
            property: 'border.width'
            to: if(txtfield.hovered){
                    return 10
                }else{
                    return 0
                }
            duration: 500
            easing.type: Easing.InOutQuint
        }    }
}

我是新来的,正在学习JS,提前感谢:D

txtfield.hovered
属性如您所料更改时,动画不会启动。要开始动画,应使用
start()
方法。解决方案1:

    TextField {
        id: txtfield

        anchors.centerIn: parent

        height: 40
        width: 250

        QtObject{

        }
        color: '#ffffff'
        placeholderTextColor: "#7fffffff"
        selectedTextColor: '#000000'
        selectionColor: '#ffffff'
        font.family: "Verdana"
        font.pointSize: 10
        placeholderText: qsTr('Enter full name...')

        MouseArea {
            anchors.fill: parent
            hoverEnabled: true
            onHoveredChanged: {
                if (containsMouse) {
                    widthAnimation.start();
                } else {
                    bg.border.width = 0;
                }
            }
            onClicked: {
                txtfield.forceActiveFocus();
            }
        }

        background: Rectangle{
            id: bg
            color: "#2c313c"
            border.color: "#aa0000"
            //        border.width: internal.dynamicWidth
            radius: 10

            PropertyAnimation{
                id: widthAnimation
                target: bg // rectangle
                property: 'border.width'
                to: 10
                duration: 500
                easing.type: Easing.InOutQuint
            }
        }
    }
现在,您可能需要在鼠标离开文本区域时制作动画。最好使用状态和转换:

    TextField {
        id: txtfield

        anchors.centerIn: parent

        height: 40
        width: 250

        QtObject{
            id: internal
        }
        color: '#ffffff'
        placeholderTextColor: "#7fffffff"
        selectedTextColor: '#000000'
        selectionColor: '#ffffff'
        font.family: "Verdana"
        font.pointSize: 10
        placeholderText: qsTr('Enter full name...')

        MouseArea {
            anchors.fill: parent
            hoverEnabled: true
            onHoveredChanged: {
                if (containsMouse) {
                    bg.state = "hovered"
                } else {
                    bg.state = "unhovered"
                }
            }
            onClicked: {
                txtfield.forceActiveFocus();
            }
        }

        background: Rectangle{
            id: bg
            color: "#2c313c"
            border.color: "#aa0000"
            radius: 10

            states: [
                State {
                    name: "hovered"
                    PropertyChanges {
                        target: bg
                        border.width: 10
                    }
                },
                State {
                    name: "unhovered"
                    PropertyChanges {
                        target: bg
                        border.width: 0
                    }
                }
            ]
            transitions: [
                Transition {
                    from: "*"
                    to: "*"
                    PropertyAnimation {
                        property: "border.width"
                        duration: 300
                        easing.type: Easing.InOutQuint
                    }
                }
            ]
        }
    }

有关状态和转换的详细信息:

txtfield.hovered
属性按预期更改时,动画将不会启动。要开始动画,应使用
start()
方法。解决方案1:

    TextField {
        id: txtfield

        anchors.centerIn: parent

        height: 40
        width: 250

        QtObject{

        }
        color: '#ffffff'
        placeholderTextColor: "#7fffffff"
        selectedTextColor: '#000000'
        selectionColor: '#ffffff'
        font.family: "Verdana"
        font.pointSize: 10
        placeholderText: qsTr('Enter full name...')

        MouseArea {
            anchors.fill: parent
            hoverEnabled: true
            onHoveredChanged: {
                if (containsMouse) {
                    widthAnimation.start();
                } else {
                    bg.border.width = 0;
                }
            }
            onClicked: {
                txtfield.forceActiveFocus();
            }
        }

        background: Rectangle{
            id: bg
            color: "#2c313c"
            border.color: "#aa0000"
            //        border.width: internal.dynamicWidth
            radius: 10

            PropertyAnimation{
                id: widthAnimation
                target: bg // rectangle
                property: 'border.width'
                to: 10
                duration: 500
                easing.type: Easing.InOutQuint
            }
        }
    }
现在,您可能需要在鼠标离开文本区域时制作动画。最好使用状态和转换:

    TextField {
        id: txtfield

        anchors.centerIn: parent

        height: 40
        width: 250

        QtObject{
            id: internal
        }
        color: '#ffffff'
        placeholderTextColor: "#7fffffff"
        selectedTextColor: '#000000'
        selectionColor: '#ffffff'
        font.family: "Verdana"
        font.pointSize: 10
        placeholderText: qsTr('Enter full name...')

        MouseArea {
            anchors.fill: parent
            hoverEnabled: true
            onHoveredChanged: {
                if (containsMouse) {
                    bg.state = "hovered"
                } else {
                    bg.state = "unhovered"
                }
            }
            onClicked: {
                txtfield.forceActiveFocus();
            }
        }

        background: Rectangle{
            id: bg
            color: "#2c313c"
            border.color: "#aa0000"
            radius: 10

            states: [
                State {
                    name: "hovered"
                    PropertyChanges {
                        target: bg
                        border.width: 10
                    }
                },
                State {
                    name: "unhovered"
                    PropertyChanges {
                        target: bg
                        border.width: 0
                    }
                }
            ]
            transitions: [
                Transition {
                    from: "*"
                    to: "*"
                    PropertyAnimation {
                        property: "border.width"
                        duration: 300
                        easing.type: Easing.InOutQuint
                    }
                }
            ]
        }
    }


有关状态和转换的详细信息:

这不是启动动画的方式。将
if-else
替换为just
10
,并使用
TextField
中的
onHoveredChanged
槽实际启动动画(
widthAnimation.start()
)。您可能还希望有两个动画-当鼠标进入和离开时。使用状态和转换将更容易做到这一点:刚刚实现的TextField没有
onHoveredChanged
slot。你仍然使用MouseArea来代替它。@splaytreez你能提供一个例子说明你的意思吗?@CoolClound我会发布代码这不是你应该如何开始动画的。将
if-else
替换为just
10
,并使用
TextField
中的
onHoveredChanged
槽实际启动动画(
widthAnimation.start()
)。您可能还希望有两个动画-当鼠标进入和离开时。使用状态和转换将更容易做到这一点:刚刚实现的TextField没有
onHoveredChanged
slot。你仍然使用MouseArea来代替它。@splaytreez你能举个例子说明你的意思吗?@CoolClound我会发布代码尼斯,这确实有效。你能告诉我,
from:“*”
的意思吗?@CoolCloud通常,在不同的状态之间有几个转换<代码>从和到属性指定转换必须应用于哪些状态更改(从哪个状态和到哪个状态)。在这里,显示边界的动画与隐藏边界的动画相同,因此只需一个转换即可应用于任何状态更改,这就是
*
的作用。这是一个非常糟糕的解释,所以你应该阅读文档;它会变得更清晰,这很好,我明白了。谢谢你的回答:在解决方案2中,DI确实注意到了。执行代码时,边框已高亮显示。知道为什么吗?@CoolCloud这很奇怪,因为我在运行它时没有这个问题,但它并不严重:添加
状态:“unovered”
必须修复它。很好,这确实有效。你能告诉我,
from:“*”
的意思吗?@CoolCloud通常,在不同的状态之间有几个转换<代码>从和到属性指定转换必须应用于哪些状态更改(从哪个状态和到哪个状态)。在这里,显示边界的动画与隐藏边界的动画相同,因此只需一个转换即可应用于任何状态更改,这就是
*
的作用。这是一个非常糟糕的解释,所以你应该阅读文档;它会变得更清晰,这很好,我明白了。谢谢你的回答:在解决方案2中,DI确实注意到了。执行代码时,边框已高亮显示。知道为什么吗?@CoolCloud这很奇怪,因为我在运行它时没有这个问题,但它并不严重:添加
状态:“unovered”
必须修复它。