How to get access to row data in a QTableWidget
-
I have a comboBox where the user can select a team name.
Bears
Bengals
BroncosI also have a hidden QTableWidget that contains 2 columns, first column is team name in like in comboBox, 2nd column is a position.
Bears 1
Bengals 2
Broncos 3How do I get access to column 2 in QTableWidget? I will know the team name from the comboBox, if I use findItems, that just returns me a QList variable but I need to get column 2's values.
Maybe using a QTableWidget isn't the way to go but how do I get access to column 2?
Thank You.
-
I have a comboBox where the user can select a team name.
Bears
Bengals
BroncosI also have a hidden QTableWidget that contains 2 columns, first column is team name in like in comboBox, 2nd column is a position.
Bears 1
Bengals 2
Broncos 3How do I get access to column 2 in QTableWidget? I will know the team name from the comboBox, if I use findItems, that just returns me a QList variable but I need to get column 2's values.
Maybe using a QTableWidget isn't the way to go but how do I get access to column 2?
Thank You.
@Goldglv I'm not sure I understand your question. You can access cells using http://doc.qt.io/qt-5/qtablewidget.html#item
-
@Goldglv I'm not sure I understand your question. You can access cells using http://doc.qt.io/qt-5/qtablewidget.html#item
-
@jsulm but how do I know which row to access? Say for example user selected Bengals from combo, how do I know Bengals is row 2? I don't know of a find function to return me a row number.
@Goldglv If you read the documentation you will find this: http://doc.qt.io/qt-5/qtablewidget.html#findItems
It returns a QList<QTableWidgetItem>. If you read QTableWidgetItem documentation you will find http://doc.qt.io/qt-5/qtablewidgetitem.html#rowIf this is not appropriate you can still iterate over items of the column using http://doc.qt.io/qt-5/qtablewidget.html#item and check each item until you find the one you need.
-
QVariant getSecondColumn(const QString& searchTerm) const{ for (int i=0;i<tableWidget->model()->rowCount();++i){ if(tableWidget->model()->index(i,0).data().toString().compare(searchTerm /*,Qt::CaseInsensitive*/)==0) return tableWidget->model()->index(i,1).data(); } return QVariant() }
My crusade against Q*Widget classes continues
-
QVariant getSecondColumn(const QString& searchTerm) const{ for (int i=0;i<tableWidget->model()->rowCount();++i){ if(tableWidget->model()->index(i,0).data().toString().compare(searchTerm /*,Qt::CaseInsensitive*/)==0) return tableWidget->model()->index(i,1).data(); } return QVariant() }
My crusade against Q*Widget classes continues
-
Unfortunately I never use the QStandardItem interface but rather the QAbstractItemModel one (as above) so I can't be of much help with the findrow and row methods.
@Goldglv said in How to get access to row data in a QTableWidget:
Still having trouble figuring this out....
I you tell us what you are struggling with I'll try to help however I can
-
@VRonin Thank you for the example, this is very helpful. I'm still trying to learn the language....would you be able to show me an example using the #finditems and #row? Still having trouble figuring this out....
reply
@Goldglv Did you read the documentation (I provided the links)?
First you call http://doc.qt.io/qt-5/qtablewidget.html#findItems to find the items you need.
It returns a list of QTableWidgetItem. A QTableWidgetItem has a method called row(): http://doc.qt.io/qt-5/qtablewidgetitem.html#row
It returns the row number in which the item is located.QList<QTableWidgetItem *> items = ui->table->findItems("search string", Qt::MatchExactly); for (QTableWidgetItem *item : items) { int row = item->row(); // Do whatever you want to do }
Saying "Still having trouble figuring this out" is not very helpful as we do not know what the problems are.