PDA

View Full Version : QTableView get selected row



raphaelf
4th March 2006, 16:24
QT:4.1.1

Hello everybody,

Which function should i use to get my selected row?
I need this to insert here:


int myRow = tableView->???(); //i should get my selected Row as integer
model->removeRows(myRow,1);

jpn
4th March 2006, 16:30
Through view's selection model:


QItemSelectionModel * QAbstractItemView::selectionModel () const
Returns the current selection.

raphaelf
4th March 2006, 16:45
Hi JPN!
Should this return me a integer value?Have you see somewhere a example?I have pain for construct it :o

jpn
4th March 2006, 17:04
Should this return me a integer value?Have you see somewhere a example?I have pain for construct it :o

Did you even try? :)

Anyway, for a row index as an integer you can use something like this:


// Get all selections
QModelIndexList indexes = view->selectionModel()->selection().indexes();
for (int i = 0; i < indexes.count(); ++i)
{
QModelIndex index = indexes.at(i);
// To get the row/column numbers use index.row() / index.column()
}

raphaelf
6th March 2006, 15:22
hI jpn!
I cant see where should i use index.row(); ?
It's a great pity that QTableView has not a function for that :(

for count selected rows work:


QModelIndexList indexes = ui.tableView->selectionModel()->selection().indexes();
for (int i = 0; i < indexes.count(); ++i)
{

QModelIndex index = indexes.at(i);
QMessageBox::information(this,"", QString::number(i));

}

jpn
6th March 2006, 15:41
The selection list contains an index model per every selected item (cell in a table model).
If you had selected a single row, for example, the list would contain an item per each cell on that row.
Depending on the selection mode and behaviour of your view, the selection list contents may vary a lot, of course.

So you wanted to get the "selected row". Then I suppose the selection behaviour is SelectRows and selection mode is SingleSelection..
If so, due to the fact that all indexes in the selection list would represent different cells in a same row, you could just ask index.row() from the first index in the list and ignore others..

Jojo
6th March 2006, 19:29
Hello,

if you don't want to mess around with Qts interview classes you might also intercept the tree's selection changed signal to your slot..

rivci
29th December 2010, 08:42
Hi raphaelf :)

Maybe this will help u :)




QModelIndexList selectedList = ui->tabel->selectionModel()->selectedRows();
for( int i=0; i<selectedList.count(); i++)
QMessageBox::information(this,"", QString::number(selectedList.at(i).row()));

RoyBellingan
12th July 2013, 03:35
The last suggestion is true if the selection mode is "selectrows"
Qtableview is default to selectitems.