[PyQt] processEvents on QlistWidget operations does nothing

P. Mathé pmathe at neuf.fr
Sun Jan 27 15:45:36 GMT 2008


Hello,
I am sending you this message, originally sent to the PyQt mailing list, as it seems, according to Phil's answer that the problem lies within Qt.
As suggested by Phil, I changed the program to make the urllib call from within a different thread, but, if it partially solves the problem, it repaces one line of code by 
more than fifty, makes the code almost unreadable, and disconnect the QListWidget from the QProgressBar updates. (the new code is at your disposal).
My point I that I just want that QListWidget works as expected , i.e. that when I say "addItem(...)", I see the item in the UI, now, not later , who knows when.
Regards
Pierre

On Thursday 24 January 2008, P. Mathé wrote:
> The program listed below downloads the broadcast schedule of a radio
> station for the cureent day and the three following. For each date , it
> reads an url, updates the progressBar and prints out in a QListWidget the
> date corresponding to the url just downloaded.
>
> When I run this program, the progressBar is updated as soon as each page is
> received, but the rows added into the QListWidget are displayed, all at the
> same time, only when all the pages are received, i.e. when the loop "for
> jour in jours" ends.
>
> (in the program listed below the lines commented out "self.log.update()" and/or "self.log.repaint()" change
> nothing if uncommented).
>
> #!/usr/bin/env python
> # -*- coding: utf-8 -*-
>
> from PyQt4.QtCore import *
> from PyQt4.QtGui import *
>
> from mx.DateTime import *
> import urllib
>
> class radioUi(object):
>     def setupUi(self, radioUi):
>         radioUi.setObjectName("radioUi")
>        
> radioUi.resize(QSize(QRect(0,0,268,352).size()).expandedTo(radioUi.minimumS
>izeHint()))
>
>         self.log = QListWidget(radioUi)
>         self.log.setGeometry(QRect(10,80,248,261))
>         self.log.setObjectName("log")
>
>         self.btnGo = QPushButton(radioUi)
>         self.btnGo.setGeometry(QRect(160,20,80,26))
>         self.btnGo.setObjectName("pushButton")
>         self.btnGo.setText(u"start")
>
>         self.progressBar = QProgressBar(radioUi)
>         self.progressBar.setGeometry(QRect(10,50,241,23))
>         self.progressBar.setProperty("value",QVariant(24))
>
> class radioProgDialog(QDialog, radioUi):
>     def __init__(self):
>         QDialog.__init__(self)
>         # Set up the user interface
>         self.setupUi(self)
>         self.connect(self.btnGo,SIGNAL("clicked()"),self.extraire)
>         #
>
>     def extraire(self):
>         url='http://www.radiofrance.fr/francevivace/prog/index.php?time=%u'
>         #
>         jours=[(int((now() + (j * oneDay)).ticks())) for j in range(0, 4)]
>         self.progressBar.setMaximum(len(jours))
>         self.progressBar.setValue(0)
>         print 'maxi', self.progressBar.maximum()
>         for jour in jours:
>                 print self.log.updatesEnabled()
>                 next_url=url % jour
>                 p=urllib.urlopen(next_url).read()
>                 #self.pagesEmissions.append(p)
>                 d=DateFromTicks(jour)
>                 logText=u'téléchargement %02d/%02d/%d' %
> (d.day,d.month,d.year) self.log.addItem(logText)
>                 self.progressBar.setValue(self.progressBar.value() + 1)
>                 print 'value', self.progressBar.value()
>                 #self.log.update()
>                 #self.log.repaint()
>                 QCoreApplication.processEvents(QEventLoop.AllEvents)
>                 print logText
>         print 'fini Vivace'
>
> if __name__ == "__main__":
>     import sys
>     app = QApplication(sys.argv)
>     radioProg = radioProgDialog()
>     radioProg.show()
>     sys.exit(app.exec_())
>
> Version Numbers Used
> Python 2.5.1
> Qt 4.3.2
> PyQt 4.3.1
> sip 4.7
> QScintilla 2.1
> Eric4 4.0.3 (r1529)

I think this is related to the comment in the docs for processEvents() about 
some widgets not working properly in this circumstance.

A better design would be to put the read in a separate thread and update the 
GUI by sending a custom event.

Phil



More information about the PyQt mailing list