views:

195

answers:

2

Hi..

I have a class inherited from Qwidget, now in that class I will be creating "Qlistview" object and filling up the items to view.. When the selection of items in the listview gets changed I want to get the "selectionchange" event to come..

So how can I achieve this.. pls tell me in brief...

thanks

A: 

http://doc.trolltech.com/4.6/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.


http://doc.trolltech.com/4.6/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
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
Shadow
I've revised my answer. it should work now.
Ronny
+3  A: 

Hey,

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 !

Andy M
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
Shadow
Andy M
thanks, i will look into this, if i have any problem i will get back to you, thank you.
Shadow