此代碼的工作對我來說:
MyRadioGroup.qml
import QtQuick 1.0
QtObject {
property Item selected : null
}
MyRadioButton.qml
import QtQuick 1.0
Rectangle {
id: sideButton
property string text: 'Button'
property MyRadioGroup radioGroup
color: radioGroup.selected === sideButton ? '#E2EBFC' :
(sideButtonMouseArea.containsMouse ? '#DDDDDD' : '#F4F4F4')
MouseArea {
id: sideButtonMouseArea
anchors.fill: parent
hoverEnabled: true
onClicked: sideButton.radioGroup.selected = sideButton
}
Text {
id: sideButtonLabel
text: sideButton.text
font.pixelSize: 20
font.family: 'Tahoma'
anchors.centerIn: sideButton
color: radioGroup.selected === sideButton ? '#E2EBFC' : '#787878'
}
}
main.qml
import QtQuick 1.0
Rectangle {
height: 600
width: 600
MyRadioGroup {
id: radioGroup1
}
Column {
anchors.fill: parent
MyRadioButton {
anchors { left: parent.left; right: parent.right }
text: "Button 1"
radioGroup: radioGroup1
height: 100
}
MyRadioButton {
anchors { left: parent.left; right: parent.right }
text: "Button 2"
radioGroup: radioGroup1
height: 100
}
MyRadioButton {
anchors { left: parent.left; right: parent.right }
text: "Button 3"
radioGroup: radioGroup1
height: 100
}
MyRadioButton {
anchors { left: parent.left; right: parent.right }
text: "Button 4"
radioGroup: radioGroup1
height: 100
}
}
}
它做什麼:我創建了容器MyRadioGroup
來保存當前選定的項目。然後,我聲明性地將其selected
屬性與我的MyRadioButton
-s的color
屬性綁定在一起,因此它會更新每個selected
更改。
具有說,請,你組件提供商不已經包含一些這樣的東西---也許你正在重新發明輪子。
也許你可以更具體地瞭解你的問題......也許你應該看看這個:http://www.catb.org/esr/faqs/smart-questions.html#classic – 2013-03-06 09:32:02
「我用這個矩形代替按鈕「---所以,按鈕是一個選項,但是,按鈕不是qtquick核心的一部分。你使用什麼外部庫? symbian/meego的'qt-components'還是Ubuntu手機的東西?另請檢查,也許有一個單選按鈕的標準組件? – 2013-03-06 11:29:10