2016-12-16 48 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