PDA

View Full Version : QListView (QT4.7): How can i get the selected Item



rafikingkong
1st June 2011, 19:37
Hi, i am total newbie at QT, and i'm writing a chat-application, whose GUI hast a
QListView to show a list of Chat-Partners. Ich get a problem when i want to get the selected Item(supposed only one item can be selected) in the QListView: I've read int the QT4.7-DOC from nokia, but there are no such fuctions like firstChild(), sibling(),QListViewItemIterator(), which can be used to traverse the QListView. They have been there in old QT.

Here ist the relevant code:
in Chat.h:
QStandardItemModel* buddyIconModel; //buddyIconModel cotains the buddies model.
//in the Chat.ui file is a buddyList( inQListView ) defined.
in Chat.cpp:
buddyIconModel = new QStandardItemModel();
this->findChild<QListView*>("buddyList")->setModel(buddyIconModel) ;

supposed buddyIconModel has following dat
(net1, name1)
(net2, name2)
(net3, name3)

Now i try to write a slot to handle the clicked()-signal from the buddyList. I want to get the Name of the Selected Item. which function of QListView should i use? Ich have tried with the selectedIndexes(), but it is protected and the compiler reports error.

joyer83
1st June 2011, 20:08
Does this work?


QModelIndexlist selected = buddyIconModel->selectionModel()->selectedIndexes();

rafikingkong
2nd June 2011, 19:58
Does this work?


QModelIndexlist selected = buddyIconModel->selectionModel()->selectedIndexes();

The compiler says it is Ok! Thank you!:cool:Still got two questions if you can take me forwards:
buddyIconModel ist the Mode of the View buddyList. 1)Is this the same code when ich write like this?

QModelIndexlist selected = this->findChild<QListView*>("buddyList")->selectionModel()->selectedIndexes();

2) What will the fuction return if there were no Item really selected by user? Null or a Pointer of QModelIndexlist in Null?