Search code examples
pythonpyqtqtableviewqstandarditemmodel

pyqt4: Making Qtableview reflect the changes after calling QStandardItem.setData


I'm trying to build a gui using pyqt4 and am having trouble updating fields on a Qtableview Here's my code:

table=QTableView(myqMainWindow)
model=QStandardItemModel(0,1,table)
item1= QStandardItem("123")
model.setItem(0,0,item1)
table.setModel(model) 
item1.setData("321")

Now the problem I'm facing is that the table still shows "123" at the first column. What is it that I'm missing to get the table reflect the change?


Solution

  • Take a closer look at setData documentation.

    setData take two arguments : data and role. Unless you want to define a custom role you must specify one. In your case, you should use DisplayRole

    item1.setData("321", QtCore.Qt.DisplayRole)
    

    Alternatively, you could use setText.

    item1.setText("321")