2016-11-22 15 views
1

我在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中物品的索引。所以,我不想更改底層模型,但實際上只是禁用了該項目,並且不允許用戶選擇它。

+1

你應該澄清你的問題。目前還不清楚你的索引是什麼意思 - 表格行或「ComboBox」一個?要禁用指定行的Combobox,您可以執行'ComboBox {enabled:styleData.row!== 2}' – folibis

+0

@folibis是否可以從指定索引的JavaScript代碼執行此操作? – Luca

+0

@folibis我可以在javascript中啓用'enabled = false',但是這會禁用整個組件, – Luca

回答

6

是否可以禁用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 
     } 
    } 
} 

如果使用快速控制1,你應該提供自己的ComboBox組件。

+0

太棒了!謝謝。 – Luca

相關問題