thnkwthprtls
thnkwthprtls

Reputation: 3487

How to get value at a given index of a QTableView

I am working in Qt 4.7, and I have a fully populated QTableView of variable number of rows and set number of columns. I have a QStringList which I need to populate with the value from a specific column, let's say the second, of each row. So far I have this:

QStringList list;

for (int i = 0; i < ui->myTableView->height()/*see note below*/; i++)
{
      list.append(/*code I still need...*/);
}

//note: I also tried it using this->ui->myTableView->model->rowCount(), not sure which is best for this...

My problem is that I can't find any function that will allow me to get the value in a QTableView given the row and column number. I don't know if I'm just overlooking it, but I've been going through the docs and can't seem to find anything. If anyone's got any ideas I'd really appreciate the help. Thanks!

Upvotes: 2

Views: 8297

Answers (1)

vahancho
vahancho

Reputation: 21220

You could try this:

QStringList list;
QAbstractItemModel *model = ui->myTableView->model();

for(int i = 0; i < model->rowCount(); i++)
{
    QModelIndex index = model->index(i, 0); // The first column data.
    list.append(index.data().toString());
}

Upvotes: 9

Related Questions