2016-11-22 30 views
1

私はTableViewColummnようにQMLでコンボボックスを持っているし、次のように私はそれを定義にある項目を無効にします。コンボボックスは、特定のインデックス

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の項目のインデックスを指定します。だから、基本的なモデルを変更したくないのですが、実際にはアイテムを無効にして、ユーザーが選択することを許可しません。

+1

質問を明確にする必要があります。テーブル行か 'ComboBox'のインデックスが何であるかは明確ではありませんか?指定した行に対してComboboxを無効にするには、 'ComboBox {enabled:styleData.row!== 2}' ' – folibis

+0

@folibisを実行することができます。インデックスを指定するjavascriptコードからこれを行うことはできますか? – Luca

+0

@folibis javascriptで 'enabled = false'を実行できますが、コンポーネント全体が無効になります。 – Luca

答えて

6

ComboBoxメニュー項目を無効にすることはできますか?ユーザーが選択できないようにすることはできますか?

もちろん、可能です。

ComboBox delegateをこのように作成するのに必要な Quick Controls 2を使用してそれを実行するには、次の

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

クイックコントロール1を使用している場合は、あなたはComboBoxコンポーネントの独自の実装を提供する必要があります。

+0

それは素晴らしいです!ありがとう。 – Luca

関連する問題