[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    Re: [PyQt] Dont understand QListView behavior after filling the QStandardItemModel is finished
From:       Maurizio Berti <maurizio.berti () gmail ! com>
Date:       2019-05-11 16:23:04
Message-ID: CAPn+-XRyFmQf1vKXU446tCY6wd5DG9VaXPGZtcJ4A8ybCKuDNg () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Il giorno sab 11 mag 2019 alle ore 17:02 Gottfried Müller <
gottfried.mueller@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

[Attachment #5 (text/html)]

<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">Il giorno sab 11 mag 2019 \
alle ore 17:02 Gottfried Müller &lt;<a \
href="mailto:gottfried.mueller@gmx.de">gottfried.mueller@gmx.de</a>&gt; ha \
scritto:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" \
style="margin:0px 0px 0px 0.8ex;border-left:1px solid \
rgb(204,204,204);padding-left:1ex">Why appears the the QListView content in the \
second case so late? Does <br> an action exist to force the display of the items \
before the sleep event?<br></blockquote><div><br></div><div>Laying out of elements \
can require some long processing, which is not done in a single event loop iteration, \
while the &quot;status&quot; update is an easier task for the application, as it only \
needs to repaint a very simple widget.</div><div>According to the sources, the layout \
process of an item view is even &quot;delayed&quot;, through the private function  \
doDelayedItemsLayout() of every  </div><div>QAbstractItemView subclass.</div><div>To \
avoid this kind of issues, just call QApplication.processEvents() periodically within \
any time consuming process.</div><div>This obviously cannot be done for your simple \
sleep() case, but you can test its result with this \
code:</div><div><br></div><div><div><font face="courier new, monospace">      def \
postProcessing(self):</font></div><div><font face="courier new, monospace">           \
self.generateItems()</font></div><div><font face="courier new, monospace">            \
t = QElapsedTimer()</font></div><div><font face="courier new, monospace">            \
t.start()</font></div><div><font face="courier new, monospace">            while \
t.elapsed() &lt; 4000:</font></div><div><font face="courier new, monospace">          \
while QApplication.hasPendingEvents():</font></div><div><span \
style="font-family:&quot;courier new&quot;,monospace">                        \
QApplication.processEvents()</span><br></div></div><div><br></div><div>You don&#39;t \
always need to check for hasPendingEvents, but in some cases it&#39;s \
better.</div><div><br></div><div>Also, since you&#39;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 \
&quot;instant&quot; 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.</div><div><br></div><div>As a side note (just python related), you \
don&#39;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.</div><div><br></div><div>Maurizio</div><div><br></div></div>-- <br><div \
dir="ltr" class="gmail_signature">È difficile avere una convinzione precisa quando \
si parla delle ragioni del cuore. - &quot;Sostiene Pereira&quot;, Antonio \
Tabucchi<br><a href="http://www.jidesk.net" \
target="_blank">http://www.jidesk.net</a></div></div></div></div>


[Attachment #6 (text/plain)]

_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
https://www.riverbankcomputing.com/mailman/listinfo/pyqt


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic