0

我在 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按键修复选择?

4

1 回答 1

0

我找到了另一个解决方案。

我重新实现select()QItemSelectionModel. 做一些检查并将Rows标志添加到初始标志就足够了。

于 2013-09-24T17:28:22.533 回答