How to retrieve data from a QTableView in Qt?

In Qt, you can use the model() method of QTableView to access the data model, then use the rowCount() and columnCount() methods of the data model to get the number of rows and columns, and finally use the index() method of the data model to retrieve the data in a cell. Here is an example code:

QTableView *tableView = new QTableView;
QStandardItemModel *model = qobject_cast<QStandardItemModel*>(tableView->model());

int rowCount = model->rowCount();
int columnCount = model->columnCount();

for(int row = 0; row < rowCount; row++) {
    for(int column = 0; column < columnCount; column++) {
        QModelIndex index = model->index(row, column);
        QVariant data = model->data(index);
        qDebug() << "Row:" << row << "Column:" << column << "Data:" << data.toString();
    }
}

Note: adjustments and adaptations may be necessary based on specific circumstances in practical application.

 

More tutorials

How to handle the row selection event in a DataGridView?(Opens in a new browser tab)

What is the usage of selectOne in MyBatis?(Opens in a new browser tab)

Spring MVC Controller(Opens in a new browser tab)

How do you add a checkbox column in a DataGridView?(Opens in a new browser tab)

How to add a button column in a DataGridView?(Opens in a new browser tab)

Leave a Reply 0

Your email address will not be published. Required fields are marked *