2016-12-16 49 views
2

我想實現一個表視圖多重選擇,使用QtQuick,允許多選在細胞水平上,啓用QAbstractItemView::SelectItemsQAbstractItemView::ExtendedSelection標誌模仿的舊式QTableView行爲。QtQuick:考慮到允許在細胞水平

我可以使用哪些QtQuick組件?

+0

你看過QML TableView http://doc.qt.io/qt-5/qml-qtquick-controls-tableview.html#details嗎? – DuKes0mE

+0

當然! GridView頁面,在Qt文檔中,關於GridView的選擇:「該屬性包含TableView的當前行選擇」 – user2417938

+0

似乎GridView只允許在行級選擇... – user2417938

回答

1

TableView只允許默認選擇行,但您可以通過自定義其單元委託(itemDelegate)來覆蓋選擇行爲。

首先你必須禁用與默認選擇行爲:

selectionMode: SelectionMode.NoSelection 

然後在itemDelegate,你可以做這樣的事情:你的TableView

 itemDelegate: Item { 
      property bool isSelected: false 

      // When user clicks on a cell, turn the isSelected flag on 
      MouseArea { 
       anchors.fill: parent 
       onClicked: isSelected = !isSelected 
      } 


      Text { 
       anchors.verticalCenter: parent.verticalCenter 

       // If this cell is selected, color the text in blue 
       color: isSelected ? "blue" : "black" 

       text: styleData.value 
      } 
     } 

要小心,因爲signals emitted當你的細胞接受鼠標事件時不起作用。但是,當然如果你需要它們,你可以很容易地實現它們。

+0

有用,謝謝!仍然缺少允許通過鼠標拖動進行選擇的方法... – user2417938

+0

應該可以在單元上定義[DropArea](http://doc.qt.io/qt-5/qml-qtquick-droparea.html) – DuKes0mE