我在 ExtendedSelection 模式下使用 QTableView 和 SelectItems 行为。
I would like to implement the following behavior:
1) When only one cell is selected - do nothing
2) When more than one cell is selected - select full row for each selected cell.
这是一个例子:
void BaseTableView::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
{
QTableView::selectionChanged(selected, deselected);
QItemSelection currentSelection = selectionModel()->selection();
//table has selection
if(!currentSelection.isEmpty())
{
QItemSelectionRange selectionRange = currentSelection.first();
//select whole row if more than one cell is selected
if(currentSelection.count() > 1 || selectionRange.height() > 1 || selectionRange.width() > 1)
{
if(!deselected.isEmpty())
selectionModel()->select(deselected, QItemSelectionModel::Deselect | QItemSelectionModel::Rows);
if(!selected.isEmpty())
selectionModel()->select(currentSelection, QItemSelectionModel::Select | QItemSelectionModel::Rows);
}
}
}
这段代码以某种方式起作用。但我找不到更好的解决方案。
问题:
1) 使用鼠标时选择闪烁。可能是由于双重呼叫selectionChanged
或类似的事情。
2) 使用 Shift 键进行选择无法正常工作。当按下shift键的选择区域被改变时deselected
总是空的。所以选择总是在增加。
实现所描述行为的更好解决方案是什么?
如何用Shift
按键修复选择?