这可能是一个愚蠢的问题,但我无论如何也想不出如何在QListView中选择给定索引的行。
QListView的父视图有一个索引(const QModelIndex & QAbstractItemView )。问题是,我不能构造具有我想要的行号的QModelIndex,因为QModelIndex的行和列字段没有转换符。
同样继承自QAbstractItemView的QTableView有一个selectRow(int row)函数,为什么在七层地狱中QListView没有这个函数呢?
好的windows窗体在它的列表视图上有SelectedIndex属性。
发布于 2009-01-20 16:18:47
通过使用提供给视图的模型的createIndex(int row,int column)函数构造QModelIndex。QModelIndexes应该只使用一次,并且必须由工厂在模型中创建。
发布于 2009-01-30 20:28:21
This应该可以帮助您入门
QModelIndex index = model->createIndex( row, column );
if ( index.isValid() )
model->selectionModel()->select( index, QItemSelectionModel::Select );
发布于 2012-03-22 20:51:17
我在Qt4.8.0 (MSVC2010编译器)基于Michael Bishop的工作示例
QStandardItemModel *Model = (QStandardItemModel *)this->ui->listView_OptionsCategories->model();
QModelIndex index = Model->index(this->ui->stackedWidget->currentIndex(), 0);
if ( index.isValid() )
this->ui->listView_OptionsCategories->selectionModel()->select( index, QItemSelectionModel::Select );
https://stackoverflow.com/questions/461870
复制相似问题