有没有办法创建自定义动画/gif QCursor? [英] Is there a way to create a custom animated / gif QCursor?
问题描述
我正在尝试创建一个 custum 动画光标,当冗长的进程发生时替换常规光标,类似于 此处 和在这里,然而,我希望我的动画能够被动画化,例如使用 gif,就像标准 Qt.WaitCursor
的情况一样.我怎样才能做到这一点?我找到了这个关于动画的解决方案系统托盘图标,但是,我没有设法调整它以使其与光标图标一起使用.
I am trying to create a custum animated cursor that replaces the regular cursor for when lengthy processes are taking place similar to here and here, however, I woud like mine to be animated, for example using a gif, just as it is the case with the standard Qt.WaitCursor
. How can I do this? I found this solution regarding animated system tray icons, however, I didn't manage to adapt it for it to work with the cursor icon.
附带说明:当我尝试按照第一个链接中的说明执行 pm.setAlphaChannel(bm)
时,它对我不起作用,并且出现以下错误:
As a side note: When I try to execute pm.setAlphaChannel(bm)
as stated in the first link it does not work for me and I get the following error:
'AttributeError: QPixmap' object has no attribute 'setAlphaChannel'
这很奇怪,因为根据 文档,QPixmap 确实有一个 setAlphaChannel
方法.
Which is odd because according to the documentation, QPixmap does have a setAlphaChannel
method.
推荐答案
一种可能的解决方案是创建一个类来处理给定小部件的光标更新.在以下示例中,按下开始按钮时设置光标,按下停止按钮时恢复光标:
One possible solution is to create a class that handles the update of the cursor of a given widget. In the following example the cursor is set when the start button is pressed and the cursor is restored when the stop button is pressed:
from PyQt5 import QtCore, QtGui, QtWidgets
class ManagerCursor(QtCore.QObject):
def __init__(self, parent=None):
super(ManagerCursor, self).__init__(parent)
self._movie = None
self._widget = None
self._last_cursor = None
def setMovie(self, movie):
if isinstance(self._movie, QtGui.QMovie):
if not self._movie != QtGui.QMovie.NotRunning:
self._movie.stop()
del self._movie
self._movie = movie
self._movie.frameChanged.connect(self.on_frameChanged)
self._movie.started.connect(self.on_started)
self._movie.finished.connect(self.restore_cursor)
def setWidget(self, widget):
self._widget = widget
@QtCore.pyqtSlot()
def on_started(self):
if self._widget is not None:
self._last_cursor = self._widget.cursor()
@QtCore.pyqtSlot()
def restore_cursor(self):
if self._widget is not None:
if self._last_cursor is not None:
self._widget.setCursor(self._last_cursor)
self._last_cursor = None
@QtCore.pyqtSlot()
def start(self):
if self._movie is not None:
self._movie.start()
@QtCore.pyqtSlot()
def stop(self):
if self._movie is not None:
self._movie.stop()
self.restore_cursor()
@QtCore.pyqtSlot()
def on_frameChanged(self):
pixmap = self._movie.currentPixmap()
cursor = QtGui.QCursor(pixmap)
if self._widget is not None:
if self._last_cursor is None:
self._last_cursor = self._widget.cursor()
self._widget.setCursor(cursor)
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
start_btn = QtWidgets.QPushButton("start", clicked=self.on_start)
stop_btn = QtWidgets.QPushButton("stop", clicked=self.on_stop)
self._manager = ManagerCursor(self)
movie = QtGui.QMovie("loading-icon.gif")
self._manager.setMovie(movie)
self._manager.setWidget(self)
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(start_btn)
lay.addWidget(stop_btn)
lay.addStretch()
@QtCore.pyqtSlot()
def on_start(self):
self._manager.start()
@QtCore.pyqtSlot()
def on_stop(self):
self._manager.stop()
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
这篇关于有没有办法创建自定义动画/gif QCursor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!