Qt/C++: Getting the data at a certain cell in a 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, tha开发者_如何学JAVAt'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?
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.
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
)
Check the data()
function provided by the model that your QTableView uses, the effect that you describe is probably observed due to a bug in it.
Try this:
QModelIndex index = ui->tblInventory->indexAt(p); // p is a QPoint you get from some where, may be you catch right click
QString codestring = "*" + index->data().toString() + "*";
精彩评论