python - QtGui.QMovie gif stops when function executes -
i want show loading gif until function completed.
the code have
self.loadinggif = qtgui.qlabel(mainwindow) movie = qtgui.qmovie("hashupdate.gif") self.loadinggif.setmovie(movie) self.loadinggif.setalignment(qtcore.qt.aligncenter) self.gridlayout_2.addwidget(self.loadinggif, 4, 1, 1, 1) movie.start() self.myfunction() the problem gif shows not playing. starts play when function completed.
how can make gif play while function executing ?
the gif stops playing because function self.myfunction blocking qt event loop. event loop part of qt (among other things) processes mouse/keyboard events, handles redrawing of widgets (like buttons when hover on them or click on them) , updating current frame displayed when playing animation.
so qt event loop responsible executing code in response various things happen in program, while executing code, can't else.
so need change code. there several solutions this:
run
myfunctionin secondary thread doesn't block qt event loop. however, not option if function interacts qt gui in way. if call qt gui methods inmyfunction(like updating label, or whatever), must never done secondary thread (if try, program randomly crash). recommended useqthreadqt rather python thread, python thread work fine if don't ever need communicate main thread function.if
myfunctionruns long time because havefor/whileloop, consider usingqtimerperiodically call code in loop. control returned qt event loop afterqtimerexecutes can deal other things (like updating animation) between iterations of loop.periodically call
qapplication.instance().processevents()during function. returns control qt event loop, may unexpected behaviour if function doing things qt.
Comments
Post a Comment