Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 4

Qt-interest Archive, January 2008
Re: [PyQt] processEvents on QlistWidget operations does nothing

Pages: Prev | 1 | 2 | Next

Message 16 in thread

> Yes I download an url page. But this is not what I call a heavy work,
this
> is the opposite as the processor
> spend most of is time waiting for the web server to answer
But "heavy" is not just CPU computation calls... waiting around doing
nothing in a blocking manner is VERY heavy to a GUI app..

> > And I suspect
> > that this is the part that got moved into a separate thread. Now
maybe
> > I've just seen far worse code, but a short QThread::run() that
iterates
> > over urls, downloads them and then emits a signal for each with
whatever
> > content is needed doesn't look ugly to me.
> This is not so simple because the emit signal must be emitted from an
> object created in a non GUI thread context: it adds an indirection to
the
> stuff.
> Here is the modified program that works (I have changed
> p=urllib.urlopen(next_url).read() for  time.sleep(2) for tests
reasons).
> Corrections to make it better designed are welcomed, but I am still
> convinced that the original one was better.
> (remember that I do not want the QApplication.processEvents in it :
> QListWidget.addItem should work normally without it)

Its quite simple... Use a queued connection and it would work fine...

But your not giving the GUI a chance to update... the event loop is NOT
processing in your original loop... The system downloads a file, and
updates the progress bar, then does the whole thing again with no calls
to the event... The processEvents says, go handle anything pending....  

However, in the model view world, adding an item will not necessary
invalidate every view in that exact event cycle... Because most of the
view do a lazy draw... they will only repaint and recalculate what us
painted after quite a bit of background computation... This is a good
thing... 

Scott

--
 [ signature omitted ] 

Message 17 in thread

Le dimanche 27 janvier 2008, Andreas Pakulat a écrit :
> On 27.01.08 19:37:05, P. Mathé wrote:
> > Le dimanche 27 janvier 2008, Andreas Pakulat a écrit :
> > > On 27.01.08 16:45:36, P. Mathé wrote:
> > > > 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.
> > > 
> > > Uhm, the QListWidget does have the item immediately, however the
> > > repainting only happens on the next run of the event loop. Calling
> > > QApplication::processEvents is known to break in certain situations, any
> > > longer-taking operation that shouldn't block the UI simply belongs into
> > > a separate thread or process. Its usage is specifically for those who
> > > know what they're doing.
> > > 
> > > Most of the time you're far better off writing a short QThread subclass
> > > that does the "hard work" and sends either a custom event or simply a
> > > signal using Qt::QueuedConnection.
> > 
> > I did it : the code is awfully ugly just to replace 1 line of code
> 
> Its not about replacing 1 line of code, its about properly designing
> your applications business logic. Moving heavy work into a thread is the
> right thing to do (or separate process). QApplication::processEvents is
> the ugly hack for those who are not able to use threads properly.
> 
> Andreas
> 

Thank you for your answer.
After reading it and Scott Aron's one, I realize that the title of my message is misleading.
It should have been : "QListWidget.addItem does not work as expected".
Introducing processEvents was just a trial to see if it could solve the pb.
Pierre

--
 [ signature omitted ] 

Message 18 in thread

> Thank you for your answer.
> After reading it and Scott Aron's one, I realize that the title of my
> message is misleading.
> It should have been : "QListWidget.addItem does not work as expected".
> Introducing processEvents was just a trial to see if it could solve
the
> pb.
> Pierre

Another note... PyQT is not flawless... your issue might just be in the
PyQT integration and NOT in QT....

Its probably not... but it could be :)

--
 [ signature omitted ]