Iterating through columns of selected row in QTableView
-
wrote on 10 May 2021, 07:47 last edited by SPlatten 5 Oct 2021, 08:09
I have a lambda slot connected to QTableView::doubleClicked:
QObject::connect(mptvRecs, &QTableView::doubleClicked, [this](const QModelIndex& crIndex) { const int intRow(crIndex.row()); const int intColumns(this->mpsiModel->columnCount()); QAbstractItemDelegate* pidRow(this->mptvRecs->itemDelegateForRow(intRow)); for( int intCol=0; intCol<intColumns; intCol++ ) { ... } });
What I want to do is iterate through each of the columns in the selected row. I'm not sure if I'm on the right track or not here...
[Edit]QObject::connect(mptvRecs, &QTableView::doubleClicked, [this](const QModelIndex& crIndex) { const int intColumns(this->mpsiModel->columnCount()); QItemSelectionModel* pobjSel(this->mptvRecs ->sectionModel()); if ( pobjSel == nullptr ) { return; } for( int intCol=0; intCol<intColumns; intCol++ ) { QModelIndexList objColumn(pobjSel->selectedRows(intCol)); ... } });
So far its progressing objColumn looks correct as when I've single stepped, both r and c are correct.
How would I get the column text / value from objColumn ?
-
I have a lambda slot connected to QTableView::doubleClicked:
QObject::connect(mptvRecs, &QTableView::doubleClicked, [this](const QModelIndex& crIndex) { const int intRow(crIndex.row()); const int intColumns(this->mpsiModel->columnCount()); QAbstractItemDelegate* pidRow(this->mptvRecs->itemDelegateForRow(intRow)); for( int intCol=0; intCol<intColumns; intCol++ ) { ... } });
What I want to do is iterate through each of the columns in the selected row. I'm not sure if I'm on the right track or not here...
[Edit]QObject::connect(mptvRecs, &QTableView::doubleClicked, [this](const QModelIndex& crIndex) { const int intColumns(this->mpsiModel->columnCount()); QItemSelectionModel* pobjSel(this->mptvRecs ->sectionModel()); if ( pobjSel == nullptr ) { return; } for( int intCol=0; intCol<intColumns; intCol++ ) { QModelIndexList objColumn(pobjSel->selectedRows(intCol)); ... } });
So far its progressing objColumn looks correct as when I've single stepped, both r and c are correct.
How would I get the column text / value from objColumn ?
wrote on 10 May 2021, 08:16 last edited by@SPlatten said in Iterating through columns of selected row in QTableView:
QModelIndexList
-
@SPlatten said in Iterating through columns of selected row in QTableView:
QModelIndexList
-
wrote on 10 May 2021, 09:17 last edited by
@SPlatten That depends. I personally never worked with QModelIndexList.
My usual use case is with QSqlTabelModel in data() method when I need to consider hidden columns before returning data for visible column. Maybe our more experienced colleagues could offer more insight.Having said that - I don't think your logic is off here. Depending what you want to do while iterating here, you should be able to at least get the data.
-
@SPlatten That depends. I personally never worked with QModelIndexList.
My usual use case is with QSqlTabelModel in data() method when I need to consider hidden columns before returning data for visible column. Maybe our more experienced colleagues could offer more insight.Having said that - I don't think your logic is off here. Depending what you want to do while iterating here, you should be able to at least get the data.
wrote on 10 May 2021, 09:51 last edited by@artwaw, thank you, done now:
const int intColumns(this->mpsiModel->columnCount()); QItemSelectionModel* pobjSel(this->mptvRecs ->sectionModel()); if ( pobjSel == nullptr ) { return; } for( int intCol=0; intCol<intColumns; intCol++ ) { QModelIndexList objColumn(pobjSel->selectedRows(intCol)); for( int c=0; c<objColumn.count(); c++ ) { QString strItem(objColumn.at(c).data().toString()); ... } }
1/5