Qt/C++:获取 QTableView 中某个单元格的数据
我正在尝试获取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
解决方法:
不太确定为什么上面的方法不起作用,但这个可以。感谢您的帮助。
Resolved with:
Not quite sure why the above doesn't work, but this does. Thanks for the help.
这也可以工作,而且更短:(
顶部使用的
model
是绑定到此tblInventory
的 QAbstractModel)This one work too and it's shorter:
(
model
used top is the QAbstractModel that is bound to thistblInventory
)检查您的 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.试试这个:
Try this: