Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
739 views
in Technique[技术] by (71.8m points)

qt - How to change a property of a object in QML upon a hover?

I have rectangle object and I need to change the background color of it when hovered. How can I do this? I tried this but didn't work

        Rectangle {
                            id: section1
                            color: "#293645"
                            radius: 10
                            anchors.fill: parent
                            anchors.rightMargin: 20
                            anchors.leftMargin: 20
                            anchors.bottomMargin: 25
                            anchors.topMargin: 25

                            MouseArea {
                                hoverEnabled: true
                                onEntered: {
                                    section1.color = "red"
                                }
                 }
question from:https://stackoverflow.com/questions/65946327/how-to-change-a-property-of-a-object-in-qml-upon-a-hover

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Just set MouseArea anchor.fill property to solve your problem.

Rectangle {
    id: section1
    color: "#293645"
    radius: 10
    anchors.fill: parent
    anchors.rightMargin: 20
    anchors.leftMargin: 20
    anchors.bottomMargin: 25
    anchors.topMargin: 25
    
    MouseArea {
        anchors.fill: parent
        hoverEnabled: true
        onEntered: {
            section1.color = "red"
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...