使用 PyQt 在 QTableView 中指定索引 [英] Specifying an index in QTableView with PyQt

查看:109
本文介绍了使用 PyQt 在 QTableView 中指定索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,我想查看特定坐标处的数据——比如第 2 行,第 5 列.我创建了一个 QPoint 对象,并设置了这些值,但是当它被转换为 QModelIndex 对象时,我得到了第 0 行和第 1 列.

I have a table and I want to see the data at a specific coordinate- say Row 2, Column 5. I create a QPoint object with those values set, but when that gets translated into a QModelIndex object, I get Row 0 and Column 1.

代码如下:

    myQPoint = QPoint()
    myQPoint.setX(2)
    myQPoint.setY(5)
    myIndex = self.view.indexAt(myQPoint) # myIndex is a QModelIndex object
    print myQPoint.x(), myQPoint.y() # 2, 5
    print myIndex.row(), myIndex.column() # 0, 1

根据文档,indexAt返回与内容坐标中位置 pos 处的表项相对应的模型项的索引位置."那么为什么 myIndex 的行和列值与 myQPoint 的 x 和 y 值不同?我究竟做错了什么?有没有办法简单地设置 QModelIndex 对象的行和列值?

According to the docs, indexAt "returns the index position of the model item corresponding to the table item at position pos in contents coordinates." Why then are the row and column values of myIndex different from the x and y values of myQPoint? What am I doing wrong? Is there a way to simply set a QModelIndex object's row and column values?

谢谢!--艾琳

推荐答案

如果要获取指定列和行的索引内容,请使用 QTableView.model().index(row, column).data().

If you want to get the contents of an index at a specified column and row, use QTableView.model().index(row, column).data().

QTableView.model(row, column) 返回一个 QModelIndex 对象 ( http://doc.qt.nokia.com/latest/qmodelindex.html ),然后有一个 QModelIndex.data(role) 方法.您可以指定您想要的角色( http://doc.qt.nokia.com/latest/qt.html#ItemDataRole-enum ),但默认为 Qt.DisplayRole(显示文本您在指定索引中看到的文本,假设您没有编辑单元格).

QTableView.model(row, column) returns a QModelIndex object ( http://doc.qt.nokia.com/latest/qmodelindex.html ), which then has a QModelIndex.data(role) method. You can specifiy which role you want ( http://doc.qt.nokia.com/latest/qt.html#ItemDataRole-enum ), but the default is Qt.DisplayRole (the display text the text you see in the specified index, assuming you are not editing the cell).

这篇关于使用 PyQt 在 QTableView 中指定索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆