McLan
McLan

Reputation: 2678

How to get the values in QModelIndexList in qt?

I have a QTableView that enables MultiSelection selectionMode following a SelectRows behavior as follow:

QSqlQueryModel model = db_manager->get_all();
ui->tableView->setModel(model);
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->tableView->setSelectionMode(QAbstractItemView::MultiSelection);
ui->tableView->show();

The selection is working properly before which the indexes of theses selected rows are put into QModelIndexList as follow:

selectedRowsIndexesList = ui->tableView->selectionModel()->selectedRows();

Now I need to extract the information where these indexes are pointing to. I do this at the moment manually as follow:

qDebug() << model->index(0,0).data().toString(); 

I change the first "0" manually. But I need to automate the process through a for-loop statement like this:

for (int i = 0; i < selectedRowsIndexesList.size(); i++){
        qDebug() << model->index(??,0).data().toString();
    }

How can I do that ?

Upvotes: 1

Views: 8228

Answers (2)

C--
C--

Reputation: 16558

The QModelIndexList is just a typedef as per the documentation. It is just a synonym for QList<QModelIndex> and you can iterate through the indexes as you would do through any QList variables.

for(int i = 0; i < mindex_list.size(); i++)
    qDebug() << mindex_list.at(i).toString();

Or something similar.

Upvotes: 0

sebastian
sebastian

Reputation: 9696

You already have the indexes in a list, so why go back to the model? You can simply access the data using the indexes you've stored:

for (int i = 0; i < selectedRowsIndexesList.size(); i++){
        qDebug() << selectedRowsIndexesList[i].data().toString();
}

Upvotes: 4

Related Questions