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 myfunction in secondary thread doesn't block qt event loop. however, not option if function interacts qt gui in way. if call qt gui methods in myfunction (like updating label, or whatever), must never done secondary thread (if try, program randomly crash). recommended use qthread qt rather python thread, python thread work fine if don't ever need communicate main thread function.

  • if myfunction runs long time because have for/while loop, consider using qtimer periodically call code in loop. control returned qt event loop after qtimer executes 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

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -