QML_Switch控件_3选2的控制算法
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.5Window {visible: truewidth: 400height: 400title: qsTr("Hello World")property int num: 0Row {spacing: 10Switch {id: switch1onCheckedChanged: {console.log(num)if(switch1.checked == true) num++;if(switch1.checked == false) num--;if(num > 2){switch1.checked = falsereturn}console.log(num)}}Switch {id: switch2onCheckedChanged: {console.log(num)if(switch2.checked == false) num--;if(switch2.checked == true) num++;if(num > 2){switch2.checked = falsereturn}console.log(num)}}Switch {id: switch3onCheckedChanged: {console.log(num)if(switch3.checked == false) num--;if(switch3.checked == true) num++;if(num > 2){switch3.checked = falsereturn}console.log(num)}}}
}
2、下面是4选3
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.5Window {visible: truewidth: 400height: 400title: qsTr("Hello World")property int num: 0Row {spacing: 10Switch {id: switch1onCheckedChanged: {console.log(num)if(switch1.checked == true) num++;if(switch1.checked == false) num--;if(num > 3){switch1.checked = falsereturn}console.log(num)}}Switch {id: switch2onCheckedChanged: {console.log(num)if(switch2.checked == false) num--;if(switch2.checked == true) num++;if(num > 3){switch2.checked = falsereturn}console.log(num)}}Switch {id: switch3onCheckedChanged: {console.log(num)if(switch3.checked == false) num--;if(switch3.checked == true) num++;if(num > 3){switch3.checked = falsereturn}console.log(num)}}Switch {id: switch4onCheckedChanged: {console.log(num)if(switch4.checked == false) num--;if(switch4.checked == true) num++;if(num > 3){switch4.checked = falsereturn}console.log(num)}}}
}
3、下面是4选2
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.5Window {visible: truewidth: 400height: 400title: qsTr("Hello World")property int num: 0Row {spacing: 10Switch {id: switch1onCheckedChanged: {console.log(num)if(switch1.checked == true) num++;if(switch1.checked == false) num--;if(num > 2){switch1.checked = falsereturn}console.log(num)}}Switch {id: switch2onCheckedChanged: {console.log(num)if(switch2.checked == false) num--;if(switch2.checked == true) num++;if(num > 2){switch2.checked = falsereturn}console.log(num)}}Switch {id: switch3onCheckedChanged: {console.log(num)if(switch3.checked == false) num--;if(switch3.checked == true) num++;if(num > 2){switch3.checked = falsereturn}console.log(num)}}Switch {id: switch4onCheckedChanged: {console.log(num)if(switch4.checked == false) num--;if(switch4.checked == true) num++;if(num > 2){switch4.checked = falsereturn}console.log(num)}}}
}