How to get a specific cell when row is clicked in my qtableview

319 Views Asked by At

I am working with a qtableview that is filled by a model and has two columns. I can get the contents of a selected cell no problem but only want the contents of the second column even if the first column is clicked.

void MainWindow::on_tableView_clicked(const QModelIndex &index)
{
    QString cellText;
    if (index.isValid()) {
            cellText = index.data().toString();
        }

    ui->lineEdit->setText(cellText);
}

the index looks like this for column 0: QModelIndex(7,0,0x55f2e5d06b00,QStandardItemModel(0x55f2e5d09740))

And for column 1: QModelIndex(8,1,0x55f2e5d06b00,QStandardItemModel(0x55f2e5d09740))

I tried to find a way to change the index for the cell that is clicked but I think there is no way to change it directly and I can not seem to find a way to tell my function to always use column 1 the second column.

Thanks for your time.

Code edited to reflect comment 1 below

{
   QString cellText;
    if (index.isValid()) {

        QModelIndex idx = index;
        idx = idx.sibling(idx.row(), 1);
        cellText = idx.data().toString();

        }

    ui->lineEdit->setText(cellText);
}```
0

There are 0 best solutions below