【问题标题】:Selecting an index in a QListView在 QListView 中选择索引
【发布时间】:2010-10-02 11:45:45
【问题描述】:

这可能是一个愚蠢的问题,但我一生都无法弄清楚如何在 QListView 中选择给定索引的行。

QAbstractItemView ,QListView 的父级有一个 setCurrentIndex(const QModelIndex &index)。问题是,我不能用我想要的行号构造一个 QModelIndex,因为 QModelIndex 的行和列字段没有修改器。

同样继承自QAbstractItemView的QTableView有一个selectRow(int row)函数,为什么七大地狱里的QListView没有这个?

好的 ol' windows 窗体的列表视图上有 SelectedIndex 属性。

【问题讨论】:

    标签: c++ user-interface qt


    【解决方案1】:

    This 应该可以帮助您入门

    QModelIndex index = model->createIndex( row, column );
    if ( index.isValid() )
        model->selectionModel()->select( index, QItemSelectionModel::Select );
    

    【讨论】:

      【解决方案2】:

      您可以使用您提供给视图的模型的 createIndex(int row, int column) 函数来构造 QModelIndex。 QModelIndexes 只能使用一次,并且必须由模型中的工厂创建。

      【讨论】:

      • 谢谢!我认为它必须是这样的!
      • QAbstractListModel::createIndex 现在受到保护。您必须使用 QAbstractListModel::index(int row, int column) 或在模型中使用 createIndex。
      【解决方案3】:

      我基于 Michael Bishop 在 Qt4.8.0 (MSVC2010 Compiller) 上的工作示例

      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 );
      

      【讨论】:

      • 这不只是选择当前项而不是任意项吗?
      • QModelIndex index = Model->index(, 0); // 在我的 QListView 中以编程方式选择工作正常
      猜你喜欢
      • 1970-01-01
      • 2018-11-12
      • 1970-01-01
      • 1970-01-01
      • 2019-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-24
      相关资源
      最近更新 更多