ComboBox disable an item at a particular index

我只是一个虾纸丫 提交于 2020-03-02 08:56:06

问题


I have a combobox in qml in a as a TableViewColummn and I define it as follows:

import QtQuick 2.3
import QtQuick.Window 2.2
import QtQuick.Layouts 1.1
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4


ListModel {
    id: comboModel

    ListElement {
        text: ""
        Index: -1
        Dims: -1
    }
}


TableViewColumn {
    id: imageTypeList
    role: "ImageType"
    title: "Image Type"
    width: 100
    delegate: Rectangle {
        ComboBox {
            anchors.verticalCenter: parent.verticalCenter
            anchors.margins: 2
            model: comboModel
            onActivated : {
                console.log(comboModel.get(index).Index)
            }
        }
    }
}

My question is that if it is possible to disable a combobox menu item given a index to the item in the ComboBox. So, I would not like to change the underlying model but actually simply disable the item and not allow the user to select it.


回答1:


Is it possible to disable a ComboBox menu item ... and not allow the user to select it?

Sure, it is possible.

To do it using Quick Controls 2 you need to create ComboBox delegate this way:

import QtQuick 2.6
import QtQuick.Window 2.2
import QtQuick.Controls 2.0

Window {
    visible: true
    width: 640
    height: 200
    title: qsTr("Let's disable some items in ComboBox")

    ComboBox {
        id: control
        currentIndex: 0
        anchors.centerIn: parent

        model: [
            { text: "Enabled item.", enabled: true },
            { text: "Supposed to be disabled. Can't click on it.", enabled: false},
            { text: "Last, but enabled item.", enabled: true}
        ]
        width: 500
        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
        }
    }
}

If you are using Quick Controls 1, you should provide your own implementation of ComboBox component.



来源:https://stackoverflow.com/questions/40742071/combobox-disable-an-item-at-a-particular-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!