Reputation: 659
I have a QTableView
instance limited to a single row selection. I don't want to care about what cell the user presses, but it should always extract the data in (selectedRow,0).
So far I am doing the following:
QModelIndexList indices = _ui->_tbl->selectionModel()->selection().indexes();
QModelIndex id = indices.at(0).sibling(indices.at(0).row(),0);
Is there a better way?
Upvotes: 1
Views: 2305
Reputation: 12321
You can get the data of the first cell of the selected row, if you go through the model
.
QModelIndex id = _ui->_tbl->model()->index(_ui->_tbl->currentIndex().row(),0);
Unfortunately Qt
does not support (and I can't figure out why) a QModelIndex
constructor with row
and column
as arguments.
Upvotes: 0
Reputation: 2189
As stated in Qt doc concerning currentIndex
:
Unless the current selection mode is NoSelection, the item is also be selected
So you can do it quicker :
QModelIndex index = _ui->_tbl->currentIndex() ;
QModelIndex id = index.sibling(index.row(),0) ;
Upvotes: 5
Reputation: 17946
Using QItemSelectionModel::selectedRows
takes out one step. It gives you the index at a particular column (o by default). Thus:
QModelIndex index = _ui->_tbl->selectionModel()->selectedRows(0).at(0);
Upvotes: 1