Qt/C++:获取 QTableView 中某个单元格的数据

发布于 2024-10-03 23:07:29 字数 858 浏览 0 评论 0原文

我正在尝试获取 QTableView 中某个单元格的文本。例如:

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

这应该获取我的 QTableView 中第 0 列第 2 行单元格中的文本。问题是,这不是它正在做的!无论我传递到 indexAt() 中的 QPoint() 的参数如何,我都会在单元格 0,0 处获取文本。我不知道为什么这是......有什么帮助吗?谢谢!

[编辑]
我也尝试过这个:

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

[编辑2] 为了弄清楚发生了什么,我输入了这行代码:

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

它应该在单元格 2,2 处获取 QModelIndex 并输出其行和列,当然应该是 2 和 2。然而,我得到了 0 和 0!所以看起来这可能是 QTableView::indexAt() 的问题,无论是我的使用还是某种错误。任何人都可以透露一些信息吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蛮可爱 2024-10-10 23:07:29

解决方法:

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

不太确定为什么上面的方法不起作用,但这个可以。感谢您的帮助。

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.

甜味拾荒者 2024-10-10 23:07:29

这也可以工作,而且更短:(

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

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

顶部使用的 model 是绑定到此 tblInventory 的 QAbstractModel)

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)

浅唱ヾ落雨殇 2024-10-10 23:07:29

检查您的 QTableView 使用的模型提供的 data() 函数,您所描述的效果可能是由于其中的错误而观察到的。

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.

秋千易 2024-10-10 23:07:29

试试这个:

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() + "*";

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() + "*";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文