开发者

How to get the selectionchange event in Qt

开发者 https://www.devze.com 2022-12-23 13:58 出处:网络
I have a class inherited from QWidget, now in that class I will be creating aQListView object and filling up the items to view.

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 开发者_JAVA技巧tell me in brief.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消