QTableView:如何设置搜索列

时间:2011-07-27 06:48:12

标签: qt search typing qtableview

我正在使用QTableView和QAbstractTableModel的子类作为其模型。 我看到(默认情况下)当用户输入内容时,QTableView开始搜索第一列中的键入文本并将视图滚动到匹配元素。这是我想要的,但不是在第一列。 我找不到告诉(代码)QTableView或QAbstractTableModel这是“搜索列”的方法。 有什么想法吗?

谢谢

2 个答案:

答案 0 :(得分:2)

QTableView通常会搜索当前具有焦点的列。只需点击要搜索的列中的单元格即可开始输入。

[编辑:]
关于您的评论:您可以使用

将任何单元格设置为活动单元格
QTableView* tableView = /* whatever */;
tableView->setCurrentIndex( const QModelIndex& index )

这也将选择单元格。如果你不想那样,你可以做

QModelIndex index = /* whatever */;
tableView->selectionModel()->setCurrentIndex( index, QItemSelectionModel::NoUpdate );

如果您的插槽连接到当前[Row | Column] Changed或selectionChanged信号的表视图的selectionModel(),您可能需要执行以下操作,具体取决于您的代码:

QTableView* tableView = /* whatever */;
QModelIndex index = /* current row, whatever column you want to search in */;

QItemSelectionModel* selectionModel = tableView->selectionModel();
// probably check for a NULL pointer? - not really sure if this is possible

bool signalsWereBlocked = selectionModel->blockSignals( true );
selectionModel->setCurrentIndex( index );
selectionModel->blockSignals( signalsWereBlocked );

答案 1 :(得分:1)

我找到了这个解决方案:

QAbstractItemModel *model = myTableView->model();
QModelIndex index = model->index( 0, SearchColumn ); // whatever column you want to search in
myTableView->setCurrentIndex(index);
//now SearchColumn has focus and future search will operate in this column

但是如果我使用QTreeView而不是QTableView它不起作用:(