【问题标题】:ComboBox disable an item at a particular indexComboBox 禁用特定索引处的项目
【发布时间】:2016-11-22 12:37:55
【问题描述】:

我在 qml 中有一个组合框作为 TableViewColummn,我将其定义如下:

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)
            }
        }
    }
}

我的问题是,如果可以给disable 组合框菜单项提供ComboBox 中的项目索引。所以,我不想更改底层模型,但实际上只是简单地禁用该项目并且不允许用户选择它。

【问题讨论】:

  • 你应该澄清你的问题。目前尚不清楚您的意思是什么索引 - 表行或ComboBox 一个?要禁用指定行的组合框,您可以执行 ComboBox { enabled: styleData.row !== 2 }
  • @folibis 是否可以通过指定索引的 javascript 代码执行此操作?
  • @folibis 我可以在 javascript 中执行 enabled = false 但这会禁用整个组件,
  • comboModel的定义是什么?您能否将其定义为QStandardItemModel 并适应this older QComboBox technique
  • 您需要为您的ComboBox 指定delegate,您应该在其中禁用特定项目。

标签: qt qml qtquick2 qtquickcontrols


【解决方案1】:

是否可以禁用 ComboBox 菜单项...并且不允许用户选择它?

当然,这是可能的。

要使用Quick Controls 2,您需要以这种方式创建ComboBox delegate

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
        }
    }
}

如果您使用的是 Quick Controls 1,您应该提供自己的 ComboBox 组件实现。

【讨论】:

  • 太棒了!谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-20
  • 1970-01-01
  • 1970-01-01
  • 2011-05-22
  • 1970-01-01
  • 2016-02-16
相关资源
最近更新 更多