Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting an index in a QListView

This might be a stupid question, but I can't for the life of me figure out how to select the row of a given index in a QListView.

QAbstractItemView , QListView's parent has a setCurrentIndex(const QModelIndex &index). The problem is, I can't construct a QModelIndex with the row number I want since the row and column field of the QModelIndex has no mutators.

QTableView, which also inherits from QAbstractItemView has a selectRow(int row) function, why in the seven hells doesn't the QListView have this?

Good ol' windows forms has the SelectedIndex property on it's listviews.

like image 430
Nailer Avatar asked Jan 20 '09 15:01

Nailer


2 Answers

This should help you get started

QModelIndex index = model->createIndex( row, column );
if ( index.isValid() )
    model->selectionModel()->select( index, QItemSelectionModel::Select );
like image 163
Michael Bishop Avatar answered Oct 07 '22 21:10

Michael Bishop


You construct the QModelIndex by using the createIndex(int row, int column) function of the model you gave to the view. QModelIndexes should only be used once, and must be created by the factory in the model.

like image 43
Arcane Avatar answered Oct 07 '22 23:10

Arcane