Reputation:
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.
Here's the code:
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
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?
Thanks! --Erin
Upvotes: 1
Views: 11314
Reputation: 120578
Just to clarify why the code in the question doesn't work:
QTableView.indexAt
returns the model item at a point on the screen (relative to the table widget). So QPoint(2, 5)
corresponds to the point two pixels in and five pixels down from the top left corner of the tabel widget's contents - which does indeed correspond with the model item at row zero, column one!
So indexAt
is what you might use to, say, get the model item currently under the mouse cursor.
As was mentioned in DK's answer, the way to get the data at a specific row/column is to look up the index in the table view's model.
Upvotes: 2
Reputation: 5750
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)
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).
Upvotes: 5