PDA

View Full Version : How to set selected item of a QListWidget?



Lawand
4th April 2009, 23:14
I have an instance of QListWidget which gets refreshed frequently,
(by refreshing I mean removing all items then re-filling)

so after refreshing I want it to select the first item, while it is by default selecting none.

talk2amulya
4th April 2009, 23:21
this might work for u:


void QItemSelectionModel::select ( const QModelIndex & index, QItemSelectionModel::SelectionFlags command )

talk2amulya
4th April 2009, 23:22
oops i put it wrong:


void QItemSelectionModel::select ( const QModelIndex & index, QItemSelectionModel::SelectionFlags command )

Lawand
4th April 2009, 23:27
Thanks for replying,
I saw that function in Qt Assistant, but I don't really know much about the "Qt model/view framework", so I was hoping for an example that demonstrates selecting the first item...

talk2amulya
5th April 2009, 00:04
ok so, QListWidget inherits QListView..QListView inherits QAbstractItemView which is the base class of all views..QAbstractItemView provides many functions that'll allow u modify your view at the basic level..u can do what u want in a number of ways.


QListWidget *list = new QListWidget();
list->setSelectionMode(QAbstractItemView::SingleSelectio n);

QModelIndex modelIndex = list->rootIndex(); // u have to find the model index of the first item here
list->setCurrentIndex(modelIndex);

before going further ahead, give a read to http://doc.trolltech.com/4.4/model-view-introduction.html just an hour of reading this will give u a lot of information about mvc in qt

aamer4yu
5th April 2009, 07:47
so after refreshing I want it to select the first item, while it is by default selecting none.
It wont select itself since you deleted all the items. Aftter filling call this -

listWidget->item(0)->setSelected(true);
and you are done :) This is assuming you want to seleced first item, as you said. For other row, you select some other row.


QModelIndex modelIndex = list->rootIndex();
Am afraid in case of listwidget this might not be valid. Also when you talk about QListWidget, you talk in terms of rows and QListWidgetItem.
You are not concerned about the internal behaviour. QListWidget manages its own model internally, and you dont have access to it.

talk2amulya
5th April 2009, 08:36
yes, u r right, u r not concerned about those values when using QListWidget..just because the guy is new to it..i was explaining in terms of mvc itself..i wrote there are number of ways to do it :) .. plus yes rootIndex() might not be valid..i just wanted to show the use of QModelIndex..and how one should get model indexes

aamer4yu
5th April 2009, 11:46
just because the guy is new to it..i was explaining in terms of mvc itself..
Were u trying to scare him :D lolz...

Lawand
5th April 2009, 11:47
well, I tried this one:

QListWidget *list = new QListWidget();
list->setSelectionMode(QAbstractItemView::SingleSelectio n);

QModelIndex modelIndex = list->rootIndex(); // u have to find the model index of the first item here
list->setCurrentIndex(modelIndex);

but it didn't work, so I am gonna stick with this one for now:

listWidget->item(0)->setSelected(true);

and I'll try to read the model-view-introduction sometimes later...


Thanks for your help.

talk2amulya
5th April 2009, 12:23
I don't think he could have gotten scared of 4 lines of code ;) wrote it just to motivate him to read abt mvc..thats not wrong i guess..and it's not working cuz of the model index..I guess I should hv saved myself all the trouble & wrote ur one line example..but that's how it is :)