Question

I use QTableView in ExtendedSelection mode with SelectItems behavior.
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.

Here is an example:

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);
        }
    }
}  

This code somehow works. But I can't find a better solution.
Problems:
1) Blinking of selection when mouse is used. Probably due to double call of selectionChanged or something like this.
2) Selection with Shift key does not work as it should. When selection area with shift key pressed is changed deselected is always empty. So selection is always increases.

what is a better solution to implement described behavior?
How to fix selection with a pressed Shift key?

Was it helpful?

Solution

I found another solution.

I reimplemented select() method of QItemSelectionModel. Than it's enough to do some check and add Rows flag to the initial flag.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top