Qt/C++: Getting the data at a certain cell in a QTableView

Joseph picture Joseph · Nov 21, 2010 · Viewed 35.5k times · Source

I'm trying to get the text at a certain cell in a QTableView. For example:

QString codestring = "*" + ui->tblInventory->indexAt(QPoint(0,2)).data().toString() + "*";

This should get the text at the cell in column 0 row 2 in my QTableView. The problem is, that's not what it's doing!. Regardless of the arguments I pass into the QPoint() in the indexAt(), I get the text at cell 0,0. I have no idea why this is... any help? Thanks!

[edit]
I've also tried this:

QString codestring = "*" + ui->tblInventory->model()->data(ui->tblInventory->indexAt(QPoint(0,2))).toString() + "*";

[Edit 2] Trying to find out what's going on, I put in this line of code:

qDebug()<< ui->tblInventory->indexAt(QPoint(2,2)).row() << " and " <<  ui->tblInventory->indexAt(QPoint(2,2)).column();

It should get the QModelIndex at cell 2,2 and output its row and its column, which of course should be 2 and 2. However, I get 0 and 0! So it seems like this might be a problem with QTableView::indexAt(), whether its my usage or some sort of bug. Can anyone shed some light?

Answer

Joseph picture Joseph · Nov 21, 2010

Resolved with:

ui->tblInventory->model()->data(ui->tblInventory->model()->index(0,2)).toString()

Not quite sure why the above doesn't work, but this does. Thanks for the help.