Solved Switches and states
-
I'm trying to work with switches and states and am having a difficult time figuring it out. I have a SwipeView with 3 pages and each page has a Switch. When I "turn on" a switch on one page I want to turn the other 2 off and I just can't figure it out so I'm hoping someone can help.
Here's a sample of my code:
import QtQuick 2.4 import QtQuick.Layouts 1.3 import QtQuick.Controls 2.0 Page { id: cycles SwipeView { id: swipeView anchors.fill: parent Item { id: firstPage Switch { id: firstSwitch } } Item { id: secondPage Switch{ id: secondSwitch } } Item { id: thirdPage Switch{ id: thirdSwitch } } } states: [ State { name: "firstState" when: firstSwitch.checked = true PropertyChanges{ target: secondSwitch checked: false } PropertyChanges{ target: thirdSwitch checked: false } }, State { name: "secondState" when: secondSwitch.checked = true PropertyChanges { target: firstSwitch checked: false } PropertyChanges { target: thirdSwitch checked: false } }, State { name: "thirdState" when: thirdSwitch.checked = true PropertyChanges { target: firstSwitch checked: false } PropertyChanges { target: secondSwitch checked: false } } ] }
I have to be missing something but I can't figure out what it is. Any ideas or suggestions?
-
Try ButtonGroup:
import QtQuick 2.4 import QtQuick.Layouts 1.3 import QtQuick.Controls 2.0 Page { id: cycles ButtonGroup { id: switches } // <== SwipeView { id: swipeView anchors.fill: parent Item { id: firstPage Switch { id: firstSwitch ButtonGroup.group: switches // <== } } Item { id: secondPage Switch{ id: secondSwitch ButtonGroup.group: switches // <== } } Item { id: thirdPage Switch{ id: thirdSwitch ButtonGroup.group: switches // <== } } } }
-
Brilliant. Thank you very much. That works perfectly.