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.
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.
this might work for u:
Qt Code:
void [QTCLASS]QItemSelectionModel[/QTCLASS]::select ( const QModelIndex & index, QItemSelectionModel::SelectionFlags command )To copy to clipboard, switch view to plain text mode
oops i put it wrong:
Qt Code:
void QItemSelectionModel::select ( const QModelIndex & index, QItemSelectionModel::SelectionFlags command )To copy to clipboard, switch view to plain text mode
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...
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.
Qt Code:
QModelIndex modelIndex = list->rootIndex(); // u have to find the model index of the first item here list->setCurrentIndex(modelIndex);To copy to clipboard, switch view to plain text mode
before going further ahead, give a read to http://doc.trolltech.com/4.4/model-v...roduction.html just an hour of reading this will give u a lot of information about mvc in qt
Lawand (5th April 2009)
It wont select itself since you deleted all the items. Aftter filling call this -so after refreshing I want it to select the first item, while it is by default selecting none.
and you are done This is assuming you want to seleced first item, as you said. For other row, you select some other row.Qt Code:
listWidget->item(0)->setSelected(true);To copy to clipboard, switch view to plain text mode
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.QModelIndex modelIndex = list->rootIndex();
You are not concerned about the internal behaviour. QListWidget manages its own model internally, and you dont have access to it.
Lawand (5th April 2009)
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
Were u trying to scare him lolz...just because the guy is new to it..i was explaining in terms of mvc itself..
well, I tried this one:
Qt Code:
QModelIndex modelIndex = list->rootIndex(); // u have to find the model index of the first item here list->setCurrentIndex(modelIndex);To copy to clipboard, switch view to plain text mode
but it didn't work, so I am gonna stick with this one for now:
Qt Code:
listWidget->item(0)->setSelected(true);To copy to clipboard, switch view to plain text mode
and I'll try to read the model-view-introduction sometimes later...
Thanks for your help.
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
Bookmarks