QListView. Unselect item.
-
I have QListView widget. It have some items.
When I click on an item I show details about this item. Everything is good so far.
If I click another item, details of clicked element is shown. Everything is perfect so far.
For such behavior I use clicked(QModelIndex) signal in QListView.
Question: how to UNSELECT item? I want to click on empty area of QListView and hide details. Problem is that signal clicked(QModelIndex) is not invoked/called. -
Obviously the clicked(QModelIndex) signal is not invoked, if you did not click on an item.
You could use the signals provided by QItemSelectionModel. A pointer to an instance of that class is available for every QAbstractItemView-derived class, including QListView.
-
[quote author="Andre" date="1295272042"]Obviously the clicked(QModelIndex) signal is not invoked, if you did not click on an item.
You could use the signals provided by QItemSelectionModel. A pointer to an instance of that class is available for every QAbstractItemView-derived class, including QListView.
[/quote]
Thank you for your time!
I can't get it work. I try
@// create connect from listview selectionModel
connect( listView->selectionModel(), SIGNAL( selectionChanged( const QItemSelection &, const QItemSelection & )),
this, SLOT( selectionChanged( const QItemSelection &, const QItemSelection & ) ));// slot you created for your class ( e.g MainWindow here )
void MainWindow::selectionChanged( const QItemSelection &sel, const QItemSelection &des )
{
Q_UNUSED( des ); // keep compiler from complaining
if( sel.isEmpty() ) {
listView->selectionModel()->reset(); // reset the selectionModel to clear entries and highlighted items
// ... other post clean up if necessary .. //
}
}@
But it doesn't help. I'm trying to find smth around it, but for now it's without result. -
[quote author="Andre" date="1295347918"]Well, sel.isEmpty() on line 9 is true, then you don't actually have selected items, do you?
So what is it you want to reset, exactly? [/quote]
Method "selectionChanged" doesn't arise at all. So there is no matter what is inside method :-)
I solve my problem, but I don't like the way I did it. I set selectionMode from SingleSelection to ExtendedSelection. I don't like that user can select more then one item, but so far I can't achive unselecting.
-
Which selection mode do you have set on your list view?
bq. QAbstractItemView::SingleSelection:
When the user selects an item, any already-selected item becomes unselected, and the user cannot unselect the selected item by clicking on it.With single selection, you could subclass QListView, reimplement mousePressEvent, check if the mouse press was on a valid index (continue with the base class implementation) or not (clear the selection model and stop).
-
[quote author="Volker" date="1295352652"]Which selection mode do you have set on your list view?
bq. QAbstractItemView::SingleSelection:
When the user selects an item, any already-selected item becomes unselected, and the user cannot unselect the selected item by clicking on it.With single selection, you could subclass QListView, reimplement mousePressEvent, check if the mouse press was on a valid index (continue with the base class implementation) or not (clear the selection model and stop).[/quote]
Wow! Thanks a lot!
I just need QAbstractItemView::ContiguousSelection selection mode. -
There's currently an active "Jira Ticket":http://bugreports.qt.nokia.com/browse/QTBUG-8836 on this issue.
Since there's no way to interactively deselect item. You should take a look at it and vote if you are interested. -
[quote author="ivan.todorovich" date="1295444078"]There's currently an active "Jira Ticket":http://bugreports.qt.nokia.com/browse/QTBUG-8836 on this issue.
Since there's no way to interactively deselect item. You should take a look at it and vote if you are interested.[/quote]It explains everything. Thanks a lot. I'll be tracking this bug.
-
[quote author="Chiz_" date="1295271468"]I have QListView widget. It have some items.
When I click on an item I show details about this item. Everything is good so far.
If I click another item, details of clicked element is shown. Everything is perfect so far.
For such behavior I use clicked(QModelIndex) signal in QListView.
Question: how to UNSELECT item? I want to click on empty area of QListView and hide details. Problem is that signal clicked(QModelIndex) is not invoked/called.[/quote]How about subclassing and using something like this in your mousePressEvent?
@
QModelIndex index = indexAt(event->pos());
if (index.isValid()) {
selectionModel()->select(index, QItemSelectionModel::ClearAndSelect);
} else {
clearSelection();
}
@ -
reimplement mouseReleaseEvent instead of mousePress, since the 'click' happens when you release the mouse.
-
[quote author="Volker" date="1295554550"]As far as I know this is platform dependent. On a Mac the selction changes as soon as you press the mouse button. On windows it might be not until button release.[/quote]
Thanks for such information. I'm developing on WinXP for Linux :-)