Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

qt

qtableview

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?

like image 834
Joseph Avatar asked Nov 21 '10 07:11

Joseph


2 Answers

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.

like image 86
Joseph Avatar answered Oct 13 '22 19:10

Joseph


This one work too and it's shorter:

QModelIndex index = model->index(row, col, QModelIndex());

ui->tblInventory->model()->data(index).toString();

(model used top is the QAbstractModel that is bound to this tblInventory)

like image 20
Mahir Zukic Avatar answered Oct 13 '22 20:10

Mahir Zukic