[Solved]Multi-select of TableView
-
Can't find any properties or functions to do multi-select
TableView haven't implemented this function yet?Or I miss something? -
Multi select is not yet a built-in feature but I made an example showing how you can do it here:
http://pastebin.com/NMtvPcpk (MultiSelectTableView) -
@import QtQuick 2.0
import QtQuick.Controls 1.0TableView {
id: tableproperty var indexes: [] property int __clicks SystemPalette{ id: syspal } rowDelegate: Rectangle { property bool selected: table.__clicks && indexes.indexOf(styleData.row) > -1 width: parent.width ; height: 18 color: selected ? syspal.highlight : syspal.base } itemDelegate: Text { property bool selected: table.__clicks && indexes.indexOf(styleData.row) > -1 anchors.fill: parent color: selected ? syspal.highlightedText : syspal.text anchors.leftMargin: 6 verticalAlignment: Text.AlignVCenter renderType: Text.NativeRendering elide: styleData.elideMode text: styleData.value } onClicked: { var indexAt = indexes.indexOf(row) if (indexAt > -1) indexes.splice(indexAt, 1) else indexes.push(row) __clicks++ // force a re-evaluation of indexes in the delegates }
}@
-
Thanks, hope that Qt5.2 or Qt5.1.x could offer us a way to do multiselect
-
Eh, It just did... The code above is just using plain Qt 5.1.0
We cant add any new API until 5.2. (5.1.x is only for bug fixes), but the solution I suggested above should be perfectly valid so there is not really any reason to wait if you need it now. I certainly agree that there should be convenience for it though.
-
Added support for key selection and key modifiers
@
import QtQuick 2.0
import QtQuick.Controls 1.0TableView {
id: tableSystemPalette{ id: syspal } property var indexes: [] property int __clicks property int __previousRow: -1 property int __modifiers: 0 property int __firstKeyRow: -1 rowDelegate: Rectangle { property bool selected: table.__clicks && indexes.indexOf(styleData.row) > -1 width: parent.width ; height: 18 color: selected ? syspal.highlight : syspal.base } itemDelegate: Text { property bool selected: table.__clicks && indexes.indexOf(styleData.row) > -1 anchors.fill: parent color: selected ? syspal.highlightedText : syspal.text anchors.leftMargin: 6 verticalAlignment: Text.AlignVCenter renderType: Text.NativeRendering elide: styleData.elideMode text: styleData.value } // Shift-key selection is relative to the index when shift was pressed Keys.onUpPressed: keySelect(__modifiers & Qt.ShiftModifier, currentRow) Keys.onDownPressed: keySelect(__modifiers & Qt.ShiftModifier, currentRow) function keySelect(shiftPressed, row) { if (shiftPressed) { indexes = [] setRowRange(__modifiers & Qt.ShiftModifier, __firstKeyRow, row) } else { indexes = [row] } } Keys.onPressed: { if (event.key === Qt.Key_Control) __modifiers |= Qt.ControlModifier if (event.key === Qt.Key_Shift) { __modifiers |= Qt.ShiftModifier __firstKeyRow = currentRow } } Keys.onReleased: { if (event.key === Qt.Key_Control) __modifiers &= !Qt.ControlModifier if (event.key === Qt.Key_Shift) { __modifiers &= !Qt.ShiftModifier __firstKeyRow = -1 } } onClicked: { var index = currentRow if (__modifiers & Qt.ShiftModifier) { setRowRange(true, __previousRow, index) } else { var hasIndex = indexes.indexOf(index) !== -1 if (__modifiers & Qt.ControlModifier) { setRowRange(!hasIndex, index, index) } else { indexes = (indexes.length == 1 && hasIndex) ? [] : [index] __clicks++ // force a re-evaluation of indexes in the delegates } } __previousRow = index } function setRowRange(select, first, last) { var start = first <= last ? first : last var stop = first <= last ? last : first for (var i = start ; i <= stop ; ++ i) { var indexAt = indexes.indexOf(i) if (select) { if (indexAt < 0) indexes.push(i) } else { if (indexAt > -1) indexes.splice(indexAt, 1); } } __clicks++ // force a re-evaluation of indexes in the delegates }
}
@