3

By default, the built-in views in PyQt can auto-refresh itself when its model has been updated. I wrote my own chart view, but I don't know how to do it, I have to manually update it for many times.

Which signal should I use?

比尔盖子
  • 2,693
  • 5
  • 37
  • 53

2 Answers2

0

You need to connect your view to the dataChanged ( const QModelIndex & topLeft, const QModelIndex & bottomRight ) signal of the model:

This signal is emitted whenever the data in an existing item changes.

If the items are of the same parent, the affected ones are those between topLeft and bottomRight inclusive. If the items do not have the same parent, the behavior is undefined.

When reimplementing the setData() function, this signal must be emitted explicitly.

  • With `QTableView()` , if I `setModel(model)`, `setQuery(sql)`, then `setQuery(another_sql)`, the view will auto refresh. How can my view do the same thing? – 比尔盖子 Dec 22 '12 at 06:26
  • your view is deriving from `QAbstractItemView`? –  Dec 22 '12 at 06:40
  • Unfortunately, no. It is a wrapper of matplotlib widget. – 比尔盖子 Dec 22 '12 at 06:57
  • Maybe I need to implement more. Which signal has been emited when `setQuery()` executed? – 比尔盖子 Dec 22 '12 at 09:17
  • when the model emits the `dataChanged` signal the views `dataChanged` slot is called, you should implement it in your view –  Dec 22 '12 at 10:36
  • Okay, I will implement `dataChanged` slot. I also have another strange issue about `dataChanged`: http://stackoverflow.com/questions/14001592/pyqt-qtableview-doesnt-respond-datachanged-signal – 比尔盖子 Dec 22 '12 at 10:54
0

If you do not have the index() object from model , you can call the data changed signal from the model or outside the model, if you call from the model, follow the next step:

self.dataChanged.emit(index, index)

some models functions can get the index,for example:

def setData(self, **index**, value)

so, you can call the signal:

def setData(self, index, value):
   #do something

   self.dataChanged.emit(index, index)

The index on the signal indicates which element changed its data If you want to change, refresh or reload data from outside the model and you do not have the index, it is not necessary to indicate which element will be changed, so, you can apply the next line

self.yourmodel.dataChanged.emit(QtCore.QModelIndex(), QtCore.QModelIndex())

This will refresh all the data that has changed.