PDA

View Full Version : QTableWidget: How to "select none" with SingleSelection mode?



timewolf
10th August 2011, 09:57
If QTableWidget is set to SingleSelection, then I can NOT unselect a row by clicking outside of any row/column. Which means, as long as I selected a row, there's always a row selected, no matter where I click;

The "ExtentedSelection" allows to "select none" by clicking a blank area in the TableWidget, but it also allows multi-selection by holding the CTRL/SHIFT key, which is not what I want

Is there a way to make the TableWidget single selection which still allows "deselect a row"?

qlands
10th August 2011, 13:13
You can set the index to invalid like:

ui->tableWidget->setCurrentCell(-1,-1);

timewolf
10th August 2011, 16:43
You can set the index to invalid like:

ui->tableWidget->setCurrentCell(-1,-1);

How do you know when to call this method? Seems clicking on a blank area doesn't trigger any signals...

qlands
11th August 2011, 09:10
You said:


If QTableWidget is set to SingleSelection, then I can NOT unselect a row by clicking outside of any row/column. Which means, as long as I selected a row, there's always a row selected, no matter where I click;

So, if you want to unselect the current selected item you set the index to invalid (-1,-1).


How do you know when to call this method?

When I want to unselect any selected item....Like from a button that says "Unselect all"..

timewolf
16th August 2011, 08:18
When I want to unselect any selected item....Like from a button that says "Unselect all"..

ummm...that's not an elegant UX. Actually in other frameworks (like .Net), users doesn't have to click a button to clear the selection, they just clicks a blank area of the table widget, the selection gets cleared automatically.

uatek
18th January 2012, 08:25
I was looking for that and have found a non complete a non elegant solution by adding to my QTreeView subclass widget this method:

void MyWidgetClass::mousePressEvent(QMouseEvent *event)
{
QItemSelectionModel *seleccion = selectionModel();
seleccion->clear();

// MyWidgetClass inherits from QTreeView, replace QTreeView
QTreeView::mousePressEvent(event);
}


I think this works for single selection because after clearing the selection, the propagation to QTreeView::mousePressEvent makes a new selection when you click on a cell. I have not tried with multiple selection, but I think in that case it will not work.

Also there is another problem, my widget is q QTreeView, and when I collapse/expand a branch I also loose the selection (since QTreeView::mousePressEvent does not make a new selection in that cases).

Has anyone the right solution?

Added after 29 minutes:

I have found a more accurate (and elegant ;) ) solution:

void mousePressEvent(QMouseEvent *event)
{
QModelIndex item = indexAt(event->pos());
QTreeView::mousePressEvent(event);
if (!item.isValid())
clearSelection();
}

Now it keeps selection when expanding/collapsing branches, but unselects when clicking out of the items.