goGud
goGud

Reputation: 4333

How to get all values in QTableView?

I know if I want to take index and data of selected values in tableview like;

QModelIndexList _indexes = ui->tvDatabaseImages->selectionModel()->selectedRows();


    foreach (QModelIndex index, _indexes)
    {
        qDebug() << "TableView Index = " << QString::number(index.row());

        qDebug() << "TableView Index Value = " << index.data().toInt();
    }

However I want to get all tableview indexlist without selection. Is it possible? If yes, how I can do it ?

Upvotes: 2

Views: 4677

Answers (2)

BitMask
BitMask

Reputation: 314

In case someone wants QModelIndexList, they can get it by iterating over all rows of model.

QModelIndexList indices;
for (int i=0; i<model->rowCount(); i++){
    indices << model->indexFromItem(model->item(i, 0));
}

Make sure model is of "QStandardItemModel" type not "QAbstractItemModel". if your model is of QAbstractItemModel type you can cast it using static cast.

QStandardItemModel *currModel = static_cast<QStandardItemModel*>(model());

Upvotes: 0

Mel
Mel

Reputation: 6065

Using the model behind the QTableView:

model=myView.model()
for ( int col = 0; col < model.columnCount(); ++col ) 
  {
  for( int row = 0; row < model.rowCount(); ++row ) 
     {
     index = model.index( row, col );
     qDebug() << index.data();
     }
  }

Oddly, I didn't find anything more straightforward.

Upvotes: 4

Related Questions