[PyQt] Re: processEvents on QlistWidget operations does nothing

P. Mathé pmathe at neuf.fr
Mon Jan 28 08:58:20 GMT 2008


Thank you Scott for your answer, but unnless I didn'y understand you correctly, it does not work : the timer singleshot is fired only after the loop ends.
Here is the modified radioProgDialog class of the program wherre I have added th updateGui method and added the QTimer.singleShot(0, self.updateGui) statement :

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 updateGui(self):
        d=self.d
        logText=u'téléchargement %02d/%02d/%d' % (d.day,d.month,d.year)
        self.log.insertItem(0, QListWidgetItem(logText))
        self.progressBar.setValue(self.progressBar.value() + 1) 
        print logText
        
        
    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:
            next_url=url % jour
            #p=urllib.urlopen(next_url).read()
            time.sleep(2)
            #self.pagesEmissions.append(p)
            self.d=DateFromTicks(jour)
            QTimer.singleShot(0, self.updateGui)
        print 'fini Vivace'

Le dimanche 27 janvier 2008, Scott Aron Bloom a écrit :
> 
> You may find the following to work really well.. Of course I'm not a Python guy.. but here is how you could do it in C++...
> 
> Rather then having a loop that goes over the jours, have a slot that adds 1 time.  And add to the class, the iterator...
> 
> After all the jours are loaded, you call the following
> 
> QTimer::singleShot( 0, this, SLOT( onHandleNextJour() ) )
> 
> 0 means that the first time the event loop gets activated, the timer timesout, which is connected to the slot.
> 
> In the slot onHandleNextJour you add the result to the tree, update the progress bar value, and then if your not at the end, call the singleShot method again.
> 
> 
> What this does, is allows the event queue (including paintEvents) to occur without any hacks... 
> 
> processEvents is a HACK... if used properly and safely it can be very effective... However, I have yet to find a processEvents I couldn't reorganize to use the singlShot with 0 time timeout..
> 
> And of course the good news... it shouldn't add any code... just reorg a bit...
> 
> While Andreas is correct, using a QThread is also a 100% valid solution... It is overkill for "simple" tasks... And frankly, what, you I or Andreas considers simple for the overkill will vary on each and every project... I already have a class I re-use quite a bit that is templatized to allow for a "loop and execute" type thread model... But I don't use it 100% of the time, because of the intrinsic complexities of threading.
> 
> Scott




More information about the PyQt mailing list