QtGui.QMovie gif 在函数执行时停止 [英] QtGui.QMovie gif stops when function executes

查看:42
本文介绍了QtGui.QMovie gif 在函数执行时停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在功能完成之前显示加载 gif.

我的代码是

 self.LoadingGif = QtGui.QLabel(MainWindow)电影 = QtGui.QMovie("hashUpdate.gif")self.LoadingGif.setMovie(电影)self.LoadingGif.setAlignment(QtCore.Qt.AlignCenter)self.gridLayout_2.addWidget(self.LoadingGif, 4, 1, 1, 1)电影开始()self.MYFUNCTION()

问题是 gif 显示但没有播放.功能完成后才开始播放.
如何在函数执行时播放 gif?

解决方案

GIF 停止播放,因为您的函数 self.MYFUNCTION 阻塞了 Qt 事件循环.事件循环是 Qt 的一部分,它(除其他外)处理鼠标/键盘事件,处理小部件的重绘(例如当您将鼠标悬停在它们上方或单击它们时的按钮)并更新播放动画时显示的当前帧.

因此,Qt 事件循环负责执行您的代码以响应您程序中发生的各种事情,但在执行您的代码时,它不能做任何其他事情.

所以你需要改变你的代码.有几种解决方案:

  • 在辅助线程中运行 MYFUNCTION 以免阻塞 Qt 事件循环.但是,如果您的函数以任何方式与 Qt GUI 交互,则这不是一个选项.如果您在 MYFUNCTION 中调用任何 Qt GUI 方法(例如更新标签或其他),则绝不能从辅助线程执行此操作(如果您尝试,您的程序将随机崩溃).建议在 Qt 中使用 QThread 而不是 Python 线程,但如果您不需要从函数返回主线程,Python 线程也能正常工作.

  • 如果 MYFUNCTION 因为你有一个 for/while 循环而运行了很长时间,你可以考虑使用一个 QTimer 定期调用循环中的代码.QTimer 执行后,控制权返回给 Qt 事件循环,以便它可以在循环迭代之间处理其他事情(例如更新动画).

  • 在您的函数期间定期调用 QApplication.instance().processEvents().这也会将控制权返回给 Qt 事件循环,但如果您的函数也使用 Qt 执行操作,您可能会出现意外行为.

I want to show a loading gif until a function is completed.

The code I have is

 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 is that the gif shows but it is not playing. It starts to play only when the function is completed.
How can I make the gif play while the function is executing ?

解决方案

The GIF stops playing because your function self.MYFUNCTION is blocking the Qt Event loop. The Event loop is the part of Qt which (among other things) processes mouse/keyboard events, handles redrawing of widgets (like buttons when you hover over them or click on them) and updating the current frame displayed when playing an animation.

So the Qt event loop is responsible for executing your code in response to various things that happen in your program, but while it is executing your code, it can't do anything else.

So you need to change your code. There are several solutions to this:

  • Run MYFUNCTION in a secondary thread so it doesn't block the Qt event loop. However, this is not an option if your function interacts with the Qt GUI in any way. If you call any Qt GUI methods in MYFUNCTION (like updating a label, or whatever), this must never be done from a secondary thread (if you try, your program will randomly crash). It is recommended to use a QThread with Qt rather than a Python thread, but a Python thread will work fine if you don't ever need to communicate back to the main thread from your function.

  • If MYFUNCTION runs for a long time because you have a for/while loop, you could consider using a QTimer to periodically call the code in the loop. Control is returned to the Qt event loop after a QTimer executes so it can deal with other things (like updating your animation) between iterations of your loop.

  • Periodically call QApplication.instance().processEvents() during your function. This also returns control to the Qt event loop, but you may get unexpected behaviour if your function is also doing things with Qt.

这篇关于QtGui.QMovie gif 在函数执行时停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆