Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Switches and states
Qt 6.11 is out! See what's new in the release blog

Switches and states

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JasonS
    wrote on last edited by p3c0
    #1

    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?

    1 Reply Last reply
    0
    • jpnurmiJ Offline
      jpnurmiJ Offline
      jpnurmi
      wrote on last edited by
      #2

      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 // <==
                  }
              }
          }
      }
      
      1 Reply Last reply
      1
      • J Offline
        J Offline
        JasonS
        wrote on last edited by
        #3

        Brilliant. Thank you very much. That works perfectly.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved