How can I change the cursor shape with PyQt?
I think QApplication.setOverrideCursor is what you're looking for:
PyQt5:
from PyQt5.QtCore import Qtfrom PyQt5.QtWidgets import QApplication...QApplication.setOverrideCursor(Qt.WaitCursor)# do lengthy processQApplication.restoreOverrideCursor()
PyQt4:
from PyQt4.QtCore import Qtfrom PyQt4.QtGui import QApplication...QApplication.setOverrideCursor(Qt.WaitCursor)# do lengthy processQApplication.restoreOverrideCursor()
While Cameron's and David's answers are great for setting the wait cursor over an entire function, I find that a context manager works best for setting the wait cursor for snippets of code:
from contextlib import contextmanagerfrom PyQt4 import QtCorefrom PyQt4.QtGui import QApplication, QCursor@contextmanagerdef wait_cursor(): try: QApplication.setOverrideCursor(QCursor(QtCore.Qt.WaitCursor)) yield finally: QApplication.restoreOverrideCursor()
Then put the lengthy process code in a with block:
with wait_cursor(): # do lengthy process pass
ekhumoro's solution is correct. This solution is a modification for the sake of style. I used what ekhumor's did but used a python decorator.
from PyQt4.QtCore import Qtfrom PyQt4.QtGui import QApplication, QCursor, QMainWidgetdef waiting_effects(function): def new_function(self): QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) try: function(self) except Exception as e: raise e print("Error {}".format(e.args[0])) finally: QApplication.restoreOverrideCursor() return new_function
I can just put the decorator on any method I would like the spinner to be active on.
class MyWigdet(QMainWidget): # ... @waiting_effects def doLengthyProcess(self): # do lengthy process pass