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. qml combobox : disable item dynamically

qml combobox : disable item dynamically

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 729 Views
  • 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.
  • L Offline
    L Offline
    lorenwell
    wrote on last edited by lorenwell
    #1

    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)

    dheerendraD 1 Reply Last reply
    0
    • L lorenwell

      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)

      dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      @lorenwell

      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
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      L 1 Reply Last reply
      0
      • dheerendraD dheerendra

        @lorenwell

        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);
            }
        }
        
        L Offline
        L Offline
        lorenwell
        wrote on last edited by
        #3

        @dheerendra Thanks a lot ! work great!

        I have changed :
        textRole: "text"
        to
        textRole: "text1"

        to display selected value

        BRs

        1 Reply Last reply
        0
        • L lorenwell has marked this topic as solved on

        • Login

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