qml combobox : disable item dynamically
-
HI all,
I want to disable dynamically some item of my qml combobox.
I have try this :
import QtQuick 2.12 import QtQuick.Window 2.12 import QtQuick.Controls 2.12 Window { width: 640 height: 480 visible: true title: qsTr("Hello World") ComboBox { id: control currentIndex: 0 model: [ { text: "State0", enabled: true }, { text: "state1", enabled: (control.currentIndex === 0)? true : false}, { text: "State2", enabled: (control.currentIndex === 0)? true : false}, { text: "State3", enabled: true} ] textRole: "text" delegate: ItemDelegate { width: control.width text: modelData.text font.weight: control.currentIndex === index ? Font.DemiBold : Font.Normal highlighted: ListView.isCurrentItem enabled: modelData.enabled } onCurrentIndexChanged: { console.log("currentIndex=" + currentIndex) } } }In this example, if I am in state0, state1 and state2 are disable and all state are enable when I am not in state 0.
But I face binding loop issue and the combobox seems to be reset.Is it possible to achieve what I want?
Thanks ( I use QT 5.12.12)
-
HI all,
I want to disable dynamically some item of my qml combobox.
I have try this :
import QtQuick 2.12 import QtQuick.Window 2.12 import QtQuick.Controls 2.12 Window { width: 640 height: 480 visible: true title: qsTr("Hello World") ComboBox { id: control currentIndex: 0 model: [ { text: "State0", enabled: true }, { text: "state1", enabled: (control.currentIndex === 0)? true : false}, { text: "State2", enabled: (control.currentIndex === 0)? true : false}, { text: "State3", enabled: true} ] textRole: "text" delegate: ItemDelegate { width: control.width text: modelData.text font.weight: control.currentIndex === index ? Font.DemiBold : Font.Normal highlighted: ListView.isCurrentItem enabled: modelData.enabled } onCurrentIndexChanged: { console.log("currentIndex=" + currentIndex) } } }In this example, if I am in state0, state1 and state2 are disable and all state are enable when I am not in state 0.
But I face binding loop issue and the combobox seems to be reset.Is it possible to achieve what I want?
Thanks ( I use QT 5.12.12)
You can try something like this.
import QtQuick import QtQuick 2.12 import QtQuick.Window 2.12 import QtQuick.Controls 2.12 Window { width: 640 height: 480 visible: true title: qsTr("Hello World") color: "gray" ComboBox { id: control currentIndex: 0 model: ListModel { id : mod ListElement{ text1: "State0"; enabled1: false } ListElement{ text1: "state1"; enabled1: false } ListElement{ text1: "State2"; enabled1: false } ListElement{ text1: "State3"; enabled1: true } } textRole: "text" delegate: ItemDelegate { width: control.width text: text1 font.weight: control.currentIndex === index ? Font.DemiBold : Font.Normal highlighted: ListView.isCurrentItem enabled: enabled1 } function callMe(idx){ if (idx === 0){ mod.setProperty(0,"enabled1",true); mod.setProperty(1,"enabled1",false); mod.setProperty(2,"enabled1",false); mod.setProperty(3,"enabled1",true); for(var i=0;i<mod.count;i++) console.log( " Data = "+mod.get(i).text1) } else { for(var j=0;j<mod.count;j++) { console.log( " Data = "+mod.get(j).text1) mod.setProperty(j,"enabled1",true); } } } onCurrentIndexChanged: { console.log("currentIndex=" + currentIndex) control.callMe(currentIndex); } } Timer{ id : _t1 interval: 15000 running: true repeat: true onTriggered: { if (control.currentIndex === 0){ control.currentIndex = 2 }else { control.currentIndex = 0 } } } Component.onCompleted: { control.callMe(0); } } -
You can try something like this.
import QtQuick import QtQuick 2.12 import QtQuick.Window 2.12 import QtQuick.Controls 2.12 Window { width: 640 height: 480 visible: true title: qsTr("Hello World") color: "gray" ComboBox { id: control currentIndex: 0 model: ListModel { id : mod ListElement{ text1: "State0"; enabled1: false } ListElement{ text1: "state1"; enabled1: false } ListElement{ text1: "State2"; enabled1: false } ListElement{ text1: "State3"; enabled1: true } } textRole: "text" delegate: ItemDelegate { width: control.width text: text1 font.weight: control.currentIndex === index ? Font.DemiBold : Font.Normal highlighted: ListView.isCurrentItem enabled: enabled1 } function callMe(idx){ if (idx === 0){ mod.setProperty(0,"enabled1",true); mod.setProperty(1,"enabled1",false); mod.setProperty(2,"enabled1",false); mod.setProperty(3,"enabled1",true); for(var i=0;i<mod.count;i++) console.log( " Data = "+mod.get(i).text1) } else { for(var j=0;j<mod.count;j++) { console.log( " Data = "+mod.get(j).text1) mod.setProperty(j,"enabled1",true); } } } onCurrentIndexChanged: { console.log("currentIndex=" + currentIndex) control.callMe(currentIndex); } } Timer{ id : _t1 interval: 15000 running: true repeat: true onTriggered: { if (control.currentIndex === 0){ control.currentIndex = 2 }else { control.currentIndex = 0 } } } Component.onCompleted: { control.callMe(0); } }@dheerendra Thanks a lot ! work great!
I have changed :
textRole: "text"
to
textRole: "text1"to display selected value
BRs
-
L lorenwell has marked this topic as solved on