[PyQt] How to update an QAbstactTableModel

despen at verizon.net despen at verizon.net
Wed Dec 9 22:59:28 GMT 2009


Felix Schmidt <fschmidt at bihl-wiedemann.de> writes:

> [1]despen at verizon.net schrieb:
>
>  Simon Hibbs [2]<simon.hibbs at gmail.com> writes:
>
>  I've written a stock portfolio tracker in pyqt4. The main window which
>  is a QAbstractTableModel has an Update button. When I click it, the
>  program gets new stock prices and updates the data in the table.
>
>  My problem is that the data displayed in the table doesn't update until
>  the window loses and regains focus....
>
>
>  If you're loading the data into the database directly using SQL, then QT
>  won't be aware of the change. You could tryusing
>  QSqlRelationalTableModel.setData() to push the data into the model. It's
>  described here:
>
>
>  Thanks for the reply.
>
>  I'm not using SQL.
>  I get updated prices for stocks from Yahoo.  That changes the stuff
>  being displayed.
>
>  The init method is like this:
>
>  class MyTableModel(QAbstractTableModel):
>   def __init__(self, datain, headerdata, parent=None, *args):
> QAbstractTableModel.__init__(self, parent, *args)
> self.arraydata = datain
> self.headerdata = headerdata
>
>  and the init method gets called like this;
>
> tm = MyTableModel(self.tabledata, header, self)
>
>  My problem is getting new data from tabledata into the display.
>  The init above works but later on from a shortcut I
>  tabledata and do:
>
> tm.changeData(self.tabledata)
>
>  and changeData does:
>
>   def changeData(self, datain):
> self.emit(SIGNAL("LayoutAboutToBeChanged()"))
> self.arraydata = datain
> self.emit(SIGNAL("LayoutChanged()"))
>
>  I also tried changing the word "Layout" to "Data" but neither
>  causes the table to update after the shortcut.
>
>  If I unfocus and focus the window it does update.
>  So I think I'm getting the data into the table, it's just not
>  repainting itself.
>
>  The data method for the table looks like this:
>
...
> Try
> self.dataChanged.emit(self.createIndex(0, 0),
> self.createIndex(self.rowCount(0), self.columnCount(0)))
> respectively
> self.emit(SIGNAL("DataChanged(QModelIndex,QModelIndex)"),
> self.createIndex(0, 0), self.createIndex(self.rowCount(0)),
> self.columnCount(0))
> in your changeData Method to update all data in your QTableView.
> If you change number of rows and/or columns, keep the LayoutChanged()
> SIGNALs.

That's beautiful!  Thanks a lot.
I had to move a paren so I'll repost the changeData method that causes
updates for mailing list history:

 self.emit(SIGNAL("LayoutAboutToBeChanged()"))
 self.arraydata = datain
 self.emit(SIGNAL("LayoutChanged()"))
 self.dataChanged.emit(self.createIndex(0, 0),
                       self.createIndex(self.rowCount(0),
                                        self.columnCount(0)))
 self.emit(SIGNAL("DataChanged(QModelIndex,QModelIndex)"),
           self.createIndex(0, 0),
           self.createIndex(self.rowCount(0),
                            self.columnCount(0)))


Is  this  documented somewhere?   Using  the  above  for searching,  the
closest I find is here:

http://pastebin.ca/1708457

(Interestingly, it looks like another portfolio app.)

It deals with add/delete rows too which I will need soon.

Thanks again!


More information about the PyQt mailing list