2011-07-27 6 views
1

QTableViewとQAbstractTableModelのサブクラスをモデルとして使用しています。 私は、(デフォルトで)ユーザが何かを入力したときに、QTableViewが最初の列の型付きテキストの検索を開始し、ビューを一致する要素にスクロールするのを見ました。これは私が欲しいものですが、最初の列にはありません。 「検索列」であるQTableViewまたはQAbstractTableModel(コード)を伝える方法が見つかりません。 QTableView:検索列の設定方法

ありがとうございました

答えて

2

QTableViewは通常、現在フォーカスがある列を検索します。検索したい列のセルをクリックして、入力を開始するだけです。

[編集:]あなたのコメントについて
:また、セルを選択します

QTableView* tableView = /* whatever */; 
tableView->setCurrentIndex(const QModelIndex& index) 

これを使用して、アクティブセルに任意のセルを設定することができます。あなたはそれをしたくない場合は、あなたがスロット現在に接続している場合は

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

行うことができます[行|コラム】変更またはテーブルビューのselectionModelの()の信号をselectionChangedは、次の操作を実行したいかもしれません、あなたのコードに依存:

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

非常に有益な情報ありがとうございます – Luca

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 

をしかし、私はQTableViewの代わりにQTreeViewを使用している場合、それは動作しません:(

+0

私の編集した答えをチェックしてください。おそらく 'treeView-> selectionModel() - > setCurrentIndex(...)'も同様に動作します。しかし、ツリービューでテーブルビューと同じキーボード検索が可能かどうかはわかりませんが、 –

+0

はい、チェックしてtreeView-> selectionModel() - > setCurrentIndex(...) 。ありがとう!! – Luca

関連する問題