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.

Silas Parker
  • 8,017
  • 1
  • 28
  • 43
Naruto
  • 9,476
  • 37
  • 118
  • 201

2 Answers2

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!

Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
Andy M
  • 5,945
  • 7
  • 51
  • 96
  • 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? Thanks – Naruto Mar 18 '10 at 14:50
  • 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 : http://stackoverflow.com/questions/1785251/problem-filtering-mousepressevent-with-installeventfilter/1785307#1785307 – Andy M Mar 18 '10 at 15:23
  • thanks, i will look into this, if i have any problem i will get back to you, thank you. – Naruto Mar 19 '10 at 14:41
  • I'm using Qt 4.8 and only selectionChanged worked for me. Thanks. – Ricardo Cristian Ramirez Jul 08 '18 at 03:18
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

Ronny Brendel
  • 4,777
  • 5
  • 35
  • 55
  • 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 help – Naruto Mar 18 '10 at 09:23