Take the 2-minute tour ×

I'm still struggling with using QListView, I'm trying to select one particular row in the view and I cannot figure out how to do this.

I found a similar question on StackOverflow which recommends using the createIndex() method of the model, however this method is protected (perhaps it used to be public but is not anymore) so that doesn't work for me. Any suggestion?

share|improve this question

2 Answers 2

up vote 5 down vote accepted

You can get the index of anything by just calling

QModelIndex _IndexOfTheCellIWant _Index = _model->index(row, column, _ParentIndex);

Then you can call setCurrentIndex(_IndexOfTheCellIWant) as bruno said in his answer.

If model contains just a standard list of items as opposed to a tree structure, then it's even easier. Because we can assume that the item is a root item - no parent.

QModelIndex _IndexOfTheCellIWant _Index =_model->index(row, column);

With a tree structure it is a little trickier, because we can't just specify a row and a column, we need to specify these with respect to a parent. If you need to know about this part let me know and I'll explain more.

Only one more thing to note. Selection is based on cells, not really rows. So if you want to ensure that when the user selects a cell (or you do through code) that the whole row is selected you can do that by setting the "selectionBehavior" on the itself.

share|improve this answer

You can use QAbstractItemView::setCurrentIndex ( const QModelIndex & index )

share|improve this answer
Thanks, I didn't know about this method. It's still not clear how I should select a row though. On the doc, it's written to use createIndex() but since it's a protected method, I cannot use it. The constructor of QModelIndex doesn't let me specify a row either. Is there any other way? –  this.lau_ Aug 3 '11 at 17:27

Your Answer


