[PyQt] Animated Progress Bars in QTableView - how?
Dave Gradwell
davegradwell at yahoo.co.uk
Sun Aug 3 00:05:23 BST 2014
Hello,
Now I'm on PyQt5, setIndexWidget works properly and the (animated) progress bars move in concert with the rest of the scroll-view and table-view.
(The c++ Qt example (the Torrent application) which uses a styledItemDelegate does not animate the progress bars - so I am leaning towards persevering with progress bar widgets shoehorned into cells.)
When I initialise my application, after I have set up a tableView, I call:
""""""""""""""""
self.tableView.setItemDelegate(progressBarDelegate(self))
""""""""""""""""
progressBarDelegate looks like this:
""""""""""""""""
class progressBarDelegate(QtWidgets.QItemDelegate):
def __init__(self, parent=None, *args, **kwargs):
super(progressBarDelegate, self).__init__(parent=None, *args, **kwargs)
self.parent = parent
def paint(self, painter, option, index):
if index.column() == 1:
thisCellsWidget = self.parent.tableView.indexWidget(index)
if thisCellsWidget == None:
thisCellsWidget = QtWidgets.QProgressBar()
self.parent.tableView.setIndexWidget(index, thisCellsWidget)
""""""""""""""""
Having 'installed' the progress bars into the 'cells', I am struggling to see how to subsequently address them (to update their values).
It is tempting to update them in the paint event itself while I already have a handle on them - but this transpired to cause a recursive repaint, with the error message:
QWidget::repaint: Recursive repaint detected
(It's understandable I guess; adjusting its value would of course require a repaint.)
Then I thought that perhaps I could update them directly when data() is called on the model. I tried to get a handle on them with:
""""""""""""""""
thisCellsWidget = self.parent.tableView.indexWidget(index)
""""""""""""""""
'parent' is passed to the model when I create it, so the model can access properties of the app, such as the tableView. I can confirm that the tableView here is the same tableView into which the progress bars were 'set'.
But, for some reason, this always returns me 'None'.
So how can I address these 'indexWidgets'?
Thanks,
Dave.
--------------------------------------------
On Sat, 31/5/14, David Boddie <david at boddie.org.uk> wrote:
Subject: Re: [PyQt] Animated Progress Bars in QTableView - how?
To: pyqt at riverbankcomputing.com
Date: Saturday, 31 May, 2014, 0:58
Just out of interest, does the example actually animate the
progress bars?
> This looked great...
> http://www.bonhardapple.com/shared-with-others/first-glance.png
> ... however, when I scrolled, the progress bars didn't
move in concert with
> the rest of the scroll area. They
floated above the cells which scrolled
> away underneath them.
> http://www.bonhardapple.com/shared-with-others/scrolled.png
I'd avoid using cell widgets for this. It should be possible
with delegates.
There is no right way to do this - or official right way, at
least - because
I don't think delegates were designed with this use case in
mind. However, it
is possible to hack something together using timers. I once
wrote an example
(not using progress bars, mind) that shows animated
delegates. It's on the
sadly now defunct PyQt Wiki, but archive.org still has a
copy:
https://web.archive.org/web/20100704185712/http://www.diotavelli.net/PyQtWiki/Animated%20items%20using%20delegates
It's not ideal, but you can at least use it as a starting
point, perhaps.
Regards,
David
More information about the PyQt
mailing list