7

QML デスクトップ アプリケーションのページに 3 つのラジオ ボタンがあります。1つをチェックしたら、他のすべてのチェックを外したいと思います。

CheckableGroupヘルプで見つけたコードで使用してみました:

CheckableGroup { id: group }
Row {
 id: row
 spacing: platformStyle.paddingMedium
 RadioButton {
     id: button1
     text: "1"
     platformExclusiveGroup: group
 }
 RadioButton {
     id: button2
     text: "2"
     platformExclusiveGroup: group
 }
 RadioButton {
     id: button3
     text: "3"
     platformExclusiveGroup: group
     checked: true
 }
}

しかし、「platformExclusiveGroup は有効なプロパティ名ではありません」というエラーが表示されます 別の解決策を試しました

RadioButton {
  id: rbtnDecimal1
  width: 130
  onClicked: {
    if (checked) {
      rbtnHexadecimal1.checked=false;
      rbtnString1.checked=false;
    }
  }
  text: "Decimal Data";
  anchors.left: rbtnHexadecimal1.right
}

1 つのボタンがチェックされると、他のすべてのボタンはチェックされませんが、他のボタンはマウスを移動するまでチェックされたままになり、チェックされなくなります。

それを実装する方法はありますか?

4

2 に答える 2

5

ExclusiveGroupsにはを指定する必要がありますRadioButton

Row {
    id: row
    ExclusiveGroup { id: group }
    RadioButton {
        id: button1
        text: "1"
        exclusiveGroup: group
    }
    RadioButton {
        id: button2
        text: "2"
        exclusiveGroup: group
    }
    RadioButton {
        id: button3
        text: "3"
        exclusiveGroup: group
        checked: true
    }
}
于 2015-09-25T01:53:16.483 に答える
1

QML デスクトップ コンポーネントの RadioButton は最初のアプローチをサポートしていませんが、代わりに次の回避策を試すことができます。

property int currentIndex: 1

onCurrentIndexChanged: {
    button1.checked = currentIndex == 0
    button2.checked = currentIndex == 1
    button3.checked = currentIndex == 2
}

Row {
    RadioButton {
        id: button1
        text: "1"
        onClicked:{
            currentIndex = 0
        }
    }
    RadioButton {
        id: button2
        text: "2"
        onClicked:{
            currentIndex = 1
        }
    }
    RadioButton {
        id: button3
        text: "3"
        onClicked:{
            currentIndex = 2
        }
    }
}
于 2012-11-19T11:16:19.673 に答える