【问题标题】:QTableView real-time filteringQTableView 实时过滤
【发布时间】:2013-06-11 21:30:50
【问题描述】:

我的情况是这样的:我有 QTableView 和 LineEdit。我想实时显示 LineEdit 中包含值的数据。我想我应该使用 QSortProxyFilterModel,但我不知道该怎么做。我是这样写的:

void MainWindow::on_lineFind_textEdited(const QString &arg1)
{

QSortFilterProxyModel proxy;
proxy.setSourceModel(ui->tableView->model());
proxy.setFilterRegExp(arg1);
QModelIndex index=proxy.mapToSource(proxy.index(0,0));
if(index.isValid())
  {
    ui->tableView->selectionModel()->select(index,QItemSelectionModel::Select | QItemSelectionModel::Rows);
    ui->tableView->scrollTo(index,QAbstractItemView::EnsureVisible);
  }


}

但它不起作用(没有可见的变化)。工作原理示例:Clementine Player 播放列表。

【问题讨论】:

  • 为什么要使用硬编码的索引坐标? QModelIndex index=proxy.mapToSource(proxy.index(0,0));

标签: c++ qt filtering qtableview


【解决方案1】:

您创建QSortFilterProxyModel 并立即在您的函数中销毁它。是使用不当。您需要创建一个 QSortFilterProxyModel 对象(可能使用new),然后调用QTableView::setModel 将代理模型附加到您的视图。之后更改将生效。

在初始化中:

ui->setupUi(this);
my_model = new QStandardItemModel(); // or any other model class
proxy_model = new QSortFilterProxyModel();
ui->table_view->setModel(proxy_model);
proxy_model->setSourceModel(my_model);

在 textEdited 插槽中:

proxy_model->setFilterRegExp(arg1);

【讨论】:

  • 谁拥有这里的什么?代理模型会被视图删除还是由我负责?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-22
  • 1970-01-01
  • 1970-01-01
  • 2012-12-13
  • 1970-01-01
  • 2020-11-15
相关资源
最近更新 更多