PDA

View Full Version : مبتدی: مشکل در state ها در QML



oldboy
جمعه 26 فروردین 1390, 22:59 عصر
سلام دوستان.
من یه Button با qml درست کردم که سبز رنگه.
حالا میخوام وقتی ماوس میره روش رنگش روشن تر و وقتی کلیک میکنیم رنگش تیره تر بشه.
این کار رو با دو تا State انجام دادم ولی فقط یکی از state ها کار میکنه (اونی که اول مینویسم).
کسی میدونه مشکل کجاست؟

کد:



import Qt 4.7

Rectangle {
id: button
width: 180; height: 100; radius: 10
color: "#129c12"
property alias text: label.text
signal clicked

Text { id: label; text: "Button"; anchors.centerIn: parent}

MouseArea { id: mouseArea; anchors.fill: parent; onClicked: button.clicked()
hoverEnabled:true
}

Rectangle {
id: shade
anchors.fill: parent; radius: parent.radius; color: "black"; opacity: 0
}
Rectangle{
id: shade1
anchors.fill: parent; radius: parent.radius; color: "white"; opacity: 0
}

StateGroup
{
states:[
State {
name: "hover"; when: mouseArea.containsMouse==true
PropertyChanges { target: shade1; opacity:0.2}
},
State {
name: "pressed"; when: mouseArea.pressed == true
PropertyChanges { target:shade; opacity:0.2}

}]
}
}

oldboy
جمعه 26 فروردین 1390, 23:42 عصر
سلامی مجدد!
طبق معمول خودم با روش سعی و خطا جوابشو پیدا کردم!
اینم کدش:


import Qt 4.7

Rectangle {
id: button
width: 180; height: 100; radius: 10
color: "#129c12"
property alias text: label.text
signal clicked

Text { id: label; text: "Button"; anchors.centerIn: parent}

MouseArea { id: mouseArea; anchors.fill: parent
onClicked:button.clicked();
hoverEnabled:true
onPressed:{button.state="Pressed"}
onReleased:{button.state="Hover"}
onEntered:{button.state="Hover"}
onExited:{button.state="Normal"}
}

Rectangle {
id: shade
anchors.fill: parent; radius: parent.radius; color: "black"; opacity: 0
}

Rectangle{
id: shade1
anchors.fill: parent; radius: parent.radius; color: "white"; opacity: 0
}

states:[
State {
name: "Normal"
PropertyChanges {
target: shade
opacity:0
}
PropertyChanges {
target: shade1
opacity:0
}

}
,
State {
name: "Hover"
PropertyChanges { target: shade1; opacity:0.2}
},
State {
name: "Pressed"
PropertyChanges { target:shade; opacity:0.2}
}]

}