转载于:https://riverbankcomputing.com/pipermail/pyqt/2009-December/025321.html
>>> 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:
>
> def data(self, index, role):
> if not index.isValid():
> return QVariant()
> elif role == Qt.TextAlignmentRole:
> if (index.column() == 0):
> return Qt.AlignLeft
> return Qt.AlignRight
> elif role == Qt.BackgroundColorRole:
> symbol = self.arraydata[index.row()][0]
> if symbol == "$" :
> return QVariant(QColor(0xEE, 0xFF, 0xEE))
> shares = self.arraydata[index.row()][1]
> if shares == '0' :
> return QVariant(QColor('wheat'))
> if symbol[0] == '^' :
> return QVariant(QColor(0xEE, 0xEE, 0xF5))
> if index.row() == len(self.arraydata) - 1 :
> return QVariant(QColor('lightgray'))
> return QVariant()
> elif role == Qt.TextColorRole:
> content = self.arraydata[index.row()][index.column()]
> if content != 0 and str(content)[0] == '-' :
> return QVariant(QColor('red'))
> return QVariant()
> elif role != Qt.DisplayRole:
> return QVariant()
> return QVariant(self.arraydata[index.row()][index.column()])
> _______________________________________________
> PyQt mailing list PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
>
>
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.
文章的脚注信息由WordPress的wp-posturl插件自动生成
微信扫一扫,打赏作者吧~![[整理]how to run flask with pyqt5](http://www.jyguagua.com/wp-content/themes/begin/timthumb.php?src=http://www.jyguagua.com/wp-content/uploads/2021/03/pyqt_flask.png&w=280&h=210&zc=1)
![[已解决]LINK : fatal error LNK1158: cannot run 'rc.exe' 错误的解决办法](http://www.jyguagua.com/wp-content/themes/begin/timthumb.php?src=http://www.jyguagua.com/wp-content/uploads/2021/02/Snipaste_2021-02-17_15-18-26-1024x505.png&w=280&h=210&zc=1)
![[已解决]Python扩展模块 error: Unable to find vcvarsall.bat](http://www.jyguagua.com/wp-content/themes/begin/timthumb.php?src=http://www.jyguagua.com/wp-content/uploads/2020/11/Snipaste_2020-11-19_10-01-38.png&w=280&h=210&zc=1)
![[整理]PyQt画圆,动态变色](http://www.jyguagua.com/wp-content/themes/begin/timthumb.php?src=http://www.jyguagua.com/wp-content/uploads/2020/08/drawCircle.gif&w=280&h=210&zc=1)