Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

ComboBox problem with using model(int) and delegate(ItemDelegate)



  • Hi

    I have a problem with ComboBox. I have a C++ class that is name "program". I send some data from C++ to Qml. The data size in ComboBox can changes so I prefer use

    model: program.kategoriSayisi  //returns int value to ComboBox model. For example 9.
    

    I send values from C++ to Qml ComboBox with this code.

    text: program.kategoriAdi[index] //returns QVariantList value. I get values with index.
    

    Problems:

    • In the first openning tha qml page, I cant see values. I see empty value. But I click the ComboBox, I can see values in dropdown list.
    • When I select the value in dropdown list, I can't see the selected value. I see empty value.

    Where is my mistake?

    The code

    ComboBox {
        id: katgKutusu
        anchors.left: parent.left
        anchors.leftMargin: dp(2)
        anchors.top: eklenecekKatgYazisi.bottom
        anchors.topMargin: dp(2)
        width: parent.width*0.7
    
        textRole: "text"
    
        down: true
        model: program.kategoriSayisi // program.kategoriSayisi returns int value to ComboBox model. For example 9.
        delegate: ItemDelegate {
            id: katgObjeleri
            contentItem: Text {
                text: program.kategoriAdi[index] //program.kategoriAdi returns QVariantList value. I get values with index.
                font.pixelSize: dp(4)
                elide: Text.ElideRight
                verticalAlignment: Text.AlignVCenter
            }
    
            onClicked: {
                console.log("clicked:", program.kategoriAdi[index]) //When I click the some object in combobox, I can see the selected name
                console.log("index:", index)
                katgKutusu.currentIndex = index           //Is it wrong?
            }
        }
        currentIndex: 0 //I want to show first value in combobox
    
          onAccepted: {
              if (find(editText) === -1)
                  currentIndex = find(textAt(index))
    
              console.log("onAccepted") //never called!
              console.log("currentIndex: ", currentIndex) //never called!
          }
    }
    


  • Hello guys

    After a few try, I found the solution. I hope it helps people.

    ComboBox {
                id: katgKutusu
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.verticalCenter: parent.verticalCenter
                width: parent.width*0.5
    
                textRole: "text"            
    
                signal katgSecildi(var sira) //send index value (sira) to outside of ItemDelegate
    
                down: true
                model: program.kategoriSayisi
                delegate: ItemDelegate {
                    id: katgObjeleri
                    property alias seciliYazi: yazi.text
                    contentItem: Text {
                        id: yazi
                        text: program.kategoriAdi[index]
                        font.pixelSize: dp(3.5)
                        font.family: yazikarakteri1
                        color: tema.onplanRenk
                        elide: Text.ElideRight
                        verticalAlignment: Text.AlignVCenter
                    }
                    onClicked: {
                        katgKutusu.katgSecildi(index) //send index value to outside of ItemDelegate
                    }
                }
                onKatgSecildi: displayText = program.kategoriAdi[sira]  //assign index value of selected value to displayText'
                displayText: program.kategoriAdi[0] //in first run, it shows first value of array
            }
            }
    

Log in to reply