How to get the selectionchange event in Qt
Asked Answered
F

2

8

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.

Footy answered 18/3, 2010 at 9:4 Comment(0)
S
12

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!

Strobotron answered 18/3, 2010 at 14:40 Comment(4)
andy.. it got worked.. thaks lot.. but i am in trouble with some other problem i.e, if i do keypress on listviewitem keypress event is not coming.even though i have overrden the "keyPressEvent"methods still i am not getting event.. how to install the keypress event to listview? ThanksFooty
That's the problem with event I guess, you seem to have to use different "kind" of events... With the signals&slots, you just don't care how the selection changes, it just lets you know when it changes... Anyway, I would suggest to add an eventfilter on your listview... Have a look here : #1785751Strobotron
thanks, i will look into this, if i have any problem i will get back to you, thank you.Footy
I'm using Qt 4.8 and only selectionChanged worked for me. Thanks.Zebec
N
0

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

Narthex answered 18/3, 2010 at 9:15 Comment(1)
i tried the way you suggested its not coming.. connect is returning false and one more thing,Qt doc says selectionchaged is a virtual slot, i tried by overriding the slot.. still its not coming.. i don't no what is wrong. can you pls helpFooty

© 2022 - 2025 — McMap. All rights reserved.