2011-11-30 106 views
1

我有一個QTableView實例限於單行選擇。我不想關心用戶按下的單元格,但它應該始終提取(selectedRow,0)中的數據。從QTableView提取數據

到目前爲止,我做了以下內容:

QModelIndexList indices = _ui->_tbl->selectionModel()->selection().indexes(); 
QModelIndex id = indices.at(0).sibling(indices.at(0).row(),0); 

有沒有更好的辦法?

回答

5

如關於currentIndex Qt的文檔指出:

除非當前選擇模式是NOSELECTION,該項目也是 選擇

所以,你可以做到這一點更快:

QModelIndex index = _ui->_tbl->currentIndex() ; 
QModelIndex id = index.sibling(index.row(),0) ; 
+0

我找不到它在[DOC](http://doc.qt.nokia.com/latest/qabstractitemview.html#currentIndex)。是否有另一個我不知道的文檔? =) –

+0

@Royi我發現這句話的Qt 4.7.3 DOC'QAbstractItemView中:: setCurrentIndex()' – azf

1

使用QItemSelectionModel::selectedRows取出一步。它爲您提供特定列的索引(默認爲o)。因此:

QModelIndex index = _ui->_tbl->selectionModel()->selectedRows(0).at(0); 
0

You ca n獲取所選行的第一個單元格的數據,如果您通過model

QModelIndex id = _ui->_tbl->model()->index(_ui->_tbl->currentIndex().row(),0); 

可惜Qt不支持(我想不通爲什麼)一個QModelIndex構造與rowcolumn作爲參數。

+0

我想直接的方法來檢索部件的元素,如表格,樹,列表等。 –