How to get the selectionchange event in Qt

Naruto picture Naruto · Mar 18, 2010 · Viewed 8.2k times · Source

I have a class inherited from QWidget, now in that class I will be creating aQListView object and filling up the items to view. When the selection of items in the list view gets changed, I want to get the selectionChange event.

How can I achieve this?. Please tell me in brief.

Answer

Andy M picture Andy M · Mar 18, 2010

When you have a view, you will have a model that will be used to select item. It's called a QItemSelectionModel.

For example, with your QListView, you can get the selectionModel this way :

QItemSelectionModel* selectionModel() const;

Now, from that model, you'll be able to connect on many signals :

void currentChanged ( const QModelIndex & current, const QModelIndex & previous )
void currentColumnChanged ( const QModelIndex & current, const QModelIndex & previous )
void currentRowChanged ( const QModelIndex & current, const QModelIndex &    previous )
void selectionChanged ( const QItemSelection & selected, const QItemSelection & deselected )

I think it will help you a bit!