QML实现-按钮点击变色功能

import QtQuick 1.0
Rectangle {
    width:400
    height:400


    Rectangle {
        id:btn
        width: 122
        height: 30
        color: "red"
        Text {
            text: "Welcome to China"
        }
        MouseArea {
            anchors.fill: parent
            //onClicked: btn.state == 'clicked' ? btn.state = "" : btn.state = 'clicked';      
            onContainsMouseChanged: btn.state == 'clicked' ? btn.state = "" : btn.state = 'clicked';

        }
        states: [
            State {
                name: "clicked"
                PropertyChanges { target: btn; color: "blue" }
            }
        ]
    }

onContainsMouseChanged是MouseArea中的响应方法,关于QML中的元素查询,官方地址:
http://doc.qt.nokia.com/4.7-snapshot/qdeclarativeelements.html


你可能感兴趣的:(QT开发)