简体   繁体   中英

QTableWidget selected rows column information needed

I have a QTableWidget, which displays files.

What I want to do is be able to select 1 or multiple rows from this table and pass the first column contents of each row into a function to be able to manipulate.

QModelIndexList indexList = ui->filesTable->selectionModel()->selectedIndexes();
int row;
foreach (QModelIndex index, indexList) {
    row = index.row();
    qDebug() << row;
}

I've got this code but this passes the indexes in and I need the contents of the first column of the QTableWidget on the row or rows I select.

Thanks for any help in advance!

To get the content of a cell you need to use QModelIndex::data method:

QModelIndexList indexList = ui->filesTable->selectionModel()->selectedIndexes( );
foreach (QModelIndex index, indexList)
{
  qDebug() << index->data( Qt::DisplayRole );
}

You can retrieve more information about selected cells just changing the role. Custom models can accept custom roles.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM