[PyQt] Dont understand QListView behavior after filling the QStandardItemModel is finished

Maurizio Berti maurizio.berti at gmail.com
Sat May 11 17:23:04 BST 2019


Il giorno sab 11 mag 2019 alle ore 17:02 Gottfried Müller <
gottfried.mueller at gmx.de> ha scritto:

> Why appears the the QListView content in the second case so late? Does
> an action exist to force the display of the items before the sleep event?
>

Laying out of elements can require some long processing, which is not done
in a single event loop iteration, while the "status" update is an easier
task for the application, as it only needs to repaint a very simple widget.
According to the sources, the layout process of an item view is even
"delayed", through the private function doDelayedItemsLayout() of every
QAbstractItemView subclass.
To avoid this kind of issues, just call QApplication.processEvents()
periodically within any time consuming process.
This obviously cannot be done for your simple sleep() case, but you can
test its result with this code:

    def postProcessing(self):
        self.generateItems()
        t = QElapsedTimer()
        t.start()
        while t.elapsed() < 4000:
            while QApplication.hasPendingEvents():
                QApplication.processEvents()

You don't always need to check for hasPendingEvents, but in some cases it's
better.

Also, since you're doing that amount of work within the triggered signal,
it will obviously block the button down state until the method returns
giving back control to the caller (the button), so it might be better to
find another solution: for example, you could call the processing from an
"instant" QTimer, via QTimer.singleShot(0, self.generateItems), possibly by
disabling the QAction when you start to fill the model and reenabling it
again as soon as the processing is completed, to avoid multiple callings.

As a side note (just python related), you don't need to add a return at the
end of every function or method if you have nothing to return, as python
implicitly returns None at the end of every function or method that does
not return anything else.

Maurizio

-- 
È difficile avere una convinzione precisa quando si parla delle ragioni del
cuore. - "Sostiene Pereira", Antonio Tabucchi
http://www.jidesk.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190511/42b9ef98/attachment.html>


More information about the PyQt mailing list