3

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.

BaCaRoZzo
  • 7,502
  • 6
  • 51
  • 82
Luca
  • 10,458
  • 24
  • 107
  • 234
  • 1
    You should to clarify your question. It is not clear what index do you mean - table row or `ComboBox` one? To disable Combobox for specified rows you can do `ComboBox { enabled: styleData.row !== 2 }` – folibis Nov 22 '16 at 13:14
  • @folibis Is it possible to do this from javascript code specifying the index? – Luca Nov 22 '16 at 13:23
  • @folibis I can do `enabled = false` in javascript but this disables the whole component, – Luca Nov 22 '16 at 13:29
  • What is the definition of `comboModel`? Could you define it as a `QStandardItemModel` and adapt [this older `QComboBox` technique](http://stackoverflow.com/a/21740341/1296734)? – eclarkso Nov 22 '16 at 13:39
  • You need to specify `delegate` for your `ComboBox` where you should make specific item disabled. – NG_ Nov 22 '16 at 13:48
  • What `QtQuick.Controls` version do you use? – folibis Nov 22 '16 at 13:50
  • I am using 1.4...I am actually on pyqt 5.6. I must say my imports are not very consistent... – Luca Nov 22 '16 at 14:02

1 Answers1

11

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.

NG_
  • 6,895
  • 7
  • 45
  • 67