Learn what Felgo offers to help your business succeed. Start your free evaluation today! Felgo for Your Business

Forums

OverviewFelgo 2 Support (Qt 5) › How to create a set of checkboxes that only one of them can be checked ?

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #18954

    Yusuf Umut Piynar

    Hello I got 5 checkboxes

    AppCheckBox{

         id : optionA
         text:"A)"
         width: parent.width / 5
    
    
         }
     AppCheckBox{
         id: optionB
         width: parent.width / 5
       text:"B)"
     }
     AppCheckBox{
         id : optionC
         width: parent.width / 5
       text:"C)"
     }
     AppCheckBox{
         id : optionD
         width: parent.width / 5
       text:"D)"
     }
     AppCheckBox{
         id : optionE
         width: parent.width / 5
       text:"E)"
     }
    }

    I want user to be able to only check one of them at once.How do I can do that ?

    #18955

    Javier
    #18959

    Yusuf Umut Piynar

    I achieved my goal with this script but its the worst performance script that ever written, btw I can change it to radiobuttons later..

    if (optionA.checked == true){

            optionB.checked = false
            optionC.checked = false
            optionD.checked = false
            optionE.checked = false
        }
    
    
        if (optionB.checked == true){
            optionA.checked = false
            optionC.checked = false
            optionD.checked = false
            optionE.checked = false
        }
        if (optionC.checked == true){
            optionA.checked = false
            optionB.checked = false
            optionD.checked = false
            optionE.checked = false
        }
        if (optionD.checked == true){
            optionA.checked = false
            optionB.checked = false
            optionC.checked = false
            optionE.checked = false
        }
        if (optionE.checked == true){
            optionA.checked = false
            optionB.checked = false
            optionC.checked = false
            optionD.checked = false
        }
    #18964

    Alex
    Felgo Team

    Hi,

    you can use the exclusiveGroup property of the AppCheckBox, which is inherited from CheckBox:

    import Felgo 3.0
    import QtQuick 2.9
    import QtQuick.Controls 1.4
    
    App {
    
      NavigationStack {
      
        Page {
          title: "ExclusiveGroup"
          
          ExclusiveGroup {
            id: myGroup
          }
          
          Column { 
            anchors.centerIn: parent
            spacing: dp(20)
            
            AppCheckBox {
              text: "CB1"
              exclusiveGroup: myGroup
            } 
            AppCheckBox {
              text: "CB2"
              exclusiveGroup: myGroup
            }
          }
        }
      }
    }

    Note: To post code, please use the “Code” button in the editor.

    Cheers,
    Alex

    #24583

    Cezary
    ExclusiveGroup works almost perfect, but...
    If user click on already checked item then unchecks it and nothing in group will be checked.
    
    And to fix it just add
    
    onCheckedChanged: {
                updateChecked = !checked
            }
    #24584

    Alex
    Felgo Team

    This sounds more like a use-case for radio buttons then. Here is an example for that, using AppRadio which is based on Qt Quick Controls 2:

    import QtQuick.Controls 2.0 as QC2
    import QtQuick 2.9
    import Felgo 3.0
    
    App {
    
      NavigationStack {
      
        Page {
          title: "Checked: " + myGroup.checkedButton.text
          
          QC2.ButtonGroup {
            id: myGroup
            onCheckedButtonChanged: {
              console.debug("Checked " + checkedButton.text)
            }
          }
          
          Column { 
            anchors.centerIn: parent
            spacing: dp(20)
            
            AppRadio {
              text: "CB1"
              checked: true
              QC2.ButtonGroup.group: myGroup
            } 
            AppRadio {
              text: "CB2"
              QC2.ButtonGroup.group: myGroup
            }
          }
        }
      }
    }

    Best,
    Alex

Viewing 6 posts - 1 through 6 (of 6 total)

RSS feed for this thread

You must be logged in to reply to this topic.

Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded