Python 为什么可以';我不能在表格的中央单元格中检索我想要的数据吗?

Python 为什么可以';我不能在表格的中央单元格中检索我想要的数据吗?,python,pyqt,qabstracttablemodel,qmodelindex,Python,Pyqt,Qabstracttablemodel,Qmodelindex,基本上,在第一个函数中,我已经获得了我用鼠标选择的单元格,并且我能够 通过data()方法从中提取数据并在第一个函数中显示 但是,我想把它改成这样,当我选择一个单元格时,它将显示该行第一个单元格(第一列)的数据。因为我已经有了所选单元格的索引(currentCell),所以我只是实例化了一个新的ModelIndex对象,并将所选索引分配给它。然后我将对象的列更改为0。最后,我想使用data()mtohod检索新对象的数据,但没有任何内容。它为null。我花了很多时间在它上面,不知道问题出在哪里。

基本上,在第一个函数中,我已经获得了我用鼠标选择的单元格,并且我能够 通过data()方法从中提取数据并在第一个函数中显示

但是,我想把它改成这样,当我选择一个单元格时,它将显示该行第一个单元格(第一列)的数据。因为我已经有了所选单元格的索引(currentCell),所以我只是实例化了一个新的ModelIndex对象,并将所选索引分配给它。然后我将对象的列更改为0。最后,我想使用data()mtohod检索新对象的数据,但没有任何内容。它为null。我花了很多时间在它上面,不知道问题出在哪里。 感谢所有提供帮助和阅读的人:)


您不能像这样构造或修改
QModelIndex
实例。模特儿的工作就是创造和传递它们。您应该向模型(
.index
方法)询问该行第一列的
QModelIndex

def tbRobotChangedt(self,currentCell):

    model = currentCell.model()
    # or if you keep your model in a variable, use it.

    # .index normally takes 3 arguments.
    # row, column, parent
    # If this is a table model, you won't need the third argument.
    # because table is flat. no parents
    firstColumn = model.index(currentCell.row(), 0)

    # then get data as usual
    self.statusBar().showMessage("Selected Robot: %s" % firstColumn.data().toString())

嗯,效果很好。这是我在这里的第一篇帖子。你能如此详细地解释,真是太好了。谢谢你的帮助。@Derek:我很高兴它能帮上忙。欢迎来到SO:)。请务必检查以获得更好的体验。
def tbRobotChangedt(self,currentCell):

    model = currentCell.model()
    # or if you keep your model in a variable, use it.

    # .index normally takes 3 arguments.
    # row, column, parent
    # If this is a table model, you won't need the third argument.
    # because table is flat. no parents
    firstColumn = model.index(currentCell.row(), 0)

    # then get data as usual
    self.statusBar().showMessage("Selected Robot: %s" % firstColumn.data().toString())