1. 程式人生 > >qt tableview 選擇模式詳解 支援不連續選中

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記入