1
votes

I'm writing a small program with QT creator (QT 5.2.1) under Windows 7 (32 bit) and I'm having problems reading the informations stored in a TableView. My application has 3 elements, a TableView to store text data, a TextBrowser to show info and a buttom. I modified the TableView properties: when the user selects with the mouse a cell, the full row is selected and multiple row selection is not allowed.

The user select a row and when the buttom is pressed, I would like to read the content of a specific TableView cell and show it in a TextBrowser. In particular, I would like to know the row index of the selected row and read the content of the cell with that row index and a specific column index (example 2).

The pseudo-code is this:

void my_program::on_pushButton_clicked()

{
    ui->textBrowser->append("button pressed");
    QItemSelectionModel *select = ui->tableView->selectionModel();
    int index_row = select->selectedRows();
    int index_column = 2;
    char cell_data[30] = ??[index_row][index_column]
    ui->textBrowser->append(cell_data);
}

The main problem is that select->selectedRows() returns a QModelIndex that is a collection of indexs and I do not know how to convert it to int (since multiple selection are not allowed, it should have only one element).

I would be glad if someone can suggest me a way to proceed.

Thanks Francesco

edit:

Hi Bogdan, thanks a lot!! I succeed to read the cell content by using

ui->textBrowser->append(ui->tableView->model()->data(ui->tableView->model()->index(2,5)).toString());

this give me the content of the cell in position 2,5. not sure if this is the best way or not but it works !!. Can you be a bit more precise about how to iterate the QModeIndexList ? thanks :)

1
QModelIndexList is just QList<QModelIndex>, thus you need to see QList documentation. - Bogdan
The data is not stored in a table view, it's stored in the view's model. A view is just that - a view. - Kuba hasn't forgotten Monica
Please do not edit your own question with such comments. If you have a comment, post it as a comment under an answer. If you have a better answer yourself, you should answer your own question. - Kuba hasn't forgotten Monica
ok, sorry. It was my first message. In the future I will not edit the post. - user3449054

1 Answers

0
votes

selectedRows() returns QModelIndexList, thus you need to iterate over it and call QModelIndex::data() to get stored data.