qt tableview 选择模式详解 支持不连续选中

这是官方文档的选择模式详解
QAbstractItemView::SingleSelection
1
When the user selects an item, any already-selected item becomes unselected, and the user cannot unselect the selected item by clicking on it.
QAbstractItemView::ContiguousSelection
4
When the user selects an item in the usual way, the selection is cleared and the new item selected. However, if the user presses the Shift key while clicking on an item, all items between the current item and the clicked item are selected or unselected, depending on the state of the clicked item.
QAbstractItemView::ExtendedSelection
3
When the user selects an item in the usual way, the selection is cleared and the new item selected. However, if the user presses the Ctrl key when clicking on an item, the clicked item gets toggled and all other items are left untouched. If the user presses the Shift key while clicking on an item, all items between the current item and the clicked item are selected or unselected, depending on the state of the clicked item. Multiple items can be selected by dragging the mouse over them.
QAbstractItemView::MultiSelection
2
When the user selects an item in the usual way, the selection status of that item is toggled and the other items are left alone. Multiple items can be toggled by dragging the mouse over them.
QAbstractItemView::NoSelection
0
Items cannot be selected.


单选 1 只能有一个项目被选中
连续选 4 只能够被连续选中,例如按下shift后的效果
拓展选 3 效果和ContiguousSelection一样,同时支持按键支持其他功能,ctrl,shift
多选 2 效果和按着ctrl一样,当你单击一个项目,再单击另外项目,之前的那条不会被取消选中
无法选中 0 无法被选中


    ui->tableView_showExecuteInfo->setSelectionMode(QAbstractItemView::MultiSelection);//多选
    ui->tableView_showExecuteInfo->setSelectionBehavior(QAbstractItemView::SelectRows);//每次选择一行
这是最简单的方式来选中

更好的方法是利用QMAP记入















你可能感兴趣的:(Qt/QML)