2011-07-27 30 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); 

如果您插槽連接到您當前的[行|列]更改或參考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); 
+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 

但是,如果我用一個QTreeView則代替QTableView中的它不工作:(

+0

檢查我編輯的答案,可能是'treeView-> selectionModel() - > setCurrentIndex(...)'也會工作。我不知道樹視圖是否允許使用與表視圖相同的鍵盤搜索,但是 –

+0

是的,我檢查了並且treeView-> selectionModel() - > setCurrentIndex(...)也可以工作(並且有更多的選項) 。謝謝!! – Luca