How to get the selectionchange event in Qt

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.

Answers


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!


https://doc.qt.io/archives/qt-4.8/qlistwidget.html You might want to use QListWidget instead of view, I don't remember specifics why, but this class has these signals you want to use.


https://doc.qt.io/archives/qt-4.8/qlistwidget.html#itemSelectionChanged This is the signal you have to connect to.

Make a slot in your class declaration:

 private slots:
     void selChanged();

Fill this slot with what you want to do upon selection change. Connect the signal to this slot somewhere in your class - perhaps in the constructor of your QWidget derivative.

 connect(yourListWidget, SIGNAL(itemSelectionChanged()), this, SLOT(selChanged()));

that's it


Need Your Help

Unique xml nodes based on attribute

xml xslt

I want to use XSLT to transform a set of documents into one structure. I have the transformation working correctly to concatenate the documents. I don't know, however, whether the the documents have

How to map MySQL column "timestamp" to a field with Doctrine2

php mysql symfony datetime doctrine-orm

I created a Doctrine2 Entity and would like to map a field to timestamp column in MySQL.