C++ 在QListView中选择索引
这可能是一个愚蠢的问题,但我一辈子都不知道如何在QListView中选择给定索引的行 QListView的父级有一个setCurrentIndex(const QModelIndex&index)。问题是,我不能用我想要的行号构造QModelIndex,因为QModelIndex的row和column字段没有变元 QTableView也是从QAbstractItemView继承的,它有一个selectRow(int row)函数,为什么QListView在七个hells中没有这个函数呢C++ 在QListView中选择索引,c++,user-interface,qt,C++,User Interface,Qt,这可能是一个愚蠢的问题,但我一辈子都不知道如何在QListView中选择给定索引的行 QListView的父级有一个setCurrentIndex(const QModelIndex&index)。问题是,我不能用我想要的行号构造QModelIndex,因为QModelIndex的row和column字段没有变元 QTableView也是从QAbstractItemView继承的,它有一个selectRow(int row)函数,为什么QListView在七个hells中没有这个函数呢 好的ol
好的ol'windows窗体在其ListView上具有SelectedIndex属性。您可以使用为该视图提供的模型的createIndex(int row,int column)函数来构造QModelIndex。QModelIndexes只能使用一次,并且必须由工厂在模型中创建。应该可以帮助您开始
QModelIndex index = model->createIndex( row, column );
if ( index.isValid() )
model->selectionModel()->select( index, QItemSelectionModel::Select );
我在基于Michael Bishop的Qt4.8.0(MSVC2010编译器)上的工作示例
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 );
谢谢我以为一定是这样的!QAbstractListModel::createIndex现在受到保护。您必须使用QAbstractListModel::index(int行,int列)或在模型内部使用createIndex。这不只是选择当前项而不是任意项吗?QModelIndex=model->index(,0);//在我的QListView中,以编程方式选择“正常工作”