[SOLVED]A problem in ComboBox with image



  • Hi,
    i would like to place icon to the dropdown list, so I made some codes like this:

    ......
    
    model: ListModel {
            id: cbItems
            ListElement { text: ""; iconSource: "image1"}
            ListElement { text: ""; iconSource: "image2"}
        }
    
    __dropDownStyle: MenuStyle {
                id: dropDownMenuStyle
                frame: dropDownMenuStyleFrame
    //            __menuItemType: "comboboxitem"
                itemDelegate.label: Rectangle {
                    width: root.width - 50
                    height: root.height
                    color: "transparent"
                    Image {
                        id: operatorPic
                        width: height
                        height: operatorPictureHeight
                        anchors {
                            left: parent.left
                            leftMargin: 2
                            verticalCenter: parent.verticalCenter
                        }
                        source: operatorPic.height === 0 ? "" : styleData.iconSource
                    }
                    ITextLabel {
                        width: parent.width - operatorPic.width - 2 - 5
                        height: parent.height
                        fontSize: 20
                        fontColor: styleData.selected ? "white" : "black"
                        text: styleData.text
                        wrapMode: Text.WrapAtWordBoundaryOrAnywhere
                        horizontalAlignment: Text.AlignLeft
                        anchors.left: operatorPic.right
                        anchors.leftMargin: 5
                    }
                }
    
                itemDelegate.background: Rectangle {
                    z: 1
                    opacity: 0.5
                    height: root.height
                    color: styleData.selected ? "#0098DF" : "transparent"
                }
        }
    

    I know source: operatorPic.height === 0 ? "" : styleData.iconSource does not work but I have no idea how to deal with it.

    Does anybody tell me how to do it?
    Thanks.


  • Moderators

    Hi @beidaochuan
    You have styleData.index and access to the model. Combine it and use

    source: cbItems.get(styleData.index).iconSource
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.