有没有办法创建一个自定义的动画/gif QCursor?

2024-04-25 18:55:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个custum动画光标,当长时间的进程发生类似于here和{a2}时,它将替代常规光标,但是,我希望我的光标被动画化,例如使用gif,就像标准的Qt.WaitCursor一样。我该怎么做?我找到了关于动画系统托盘图标的this解决方案,但是,我没有设法使它适应光标图标的工作。在

另请注意:当我尝试执行第一个链接中所述的pm.setAlphaChannel(bm)时,它对我不起作用,我得到以下错误:

'AttributeError: QPixmap' object has no attribute 'setAlphaChannel'

这很奇怪,因为根据documentation,QPixmap确实有一个setAlphaChannel方法。在


Tags: a2标准here进程动画qtgif常规
1条回答
网友
1楼 · 发布于 2024-04-25 18:55:14

一种可能的解决方案是创建一个类来处理给定小部件的游标更新。在以下示例中,按下“开始”按钮时设置光标,按下“停止”按钮时光标恢复:

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_())

相关问题 更多 >