I have a simple application that runs a process that can last for several minutes before completing. I am trying to provide an indication to the user that it is processing the request - such as changing the cursor to an hourglass.
But I cannot quite get it to work right. All of my attempts have resulted in either an error or had no effect. And I seem to be calling the cursor shapes incorrectly, sincePyQt4.Qt.WaitCursor returns an error that the module does not contain it.
What is the correct way to indicate to the user that the process is running?
8 Answers8
I thinkQApplication.setOverrideCursor is what you're looking for:
PyQt6:
from PyQt6.QtCore import Qtfrom PyQt6.QtWidgets import QApplication...QApplication.setOverrideCursor(Qt.CursorShape.WaitCursor)# do lengthy processQApplication.restoreOverrideCursor()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()5 Comments
QCursor class has a copy constructor that takes aQt.CursorShape enum value, so it is not really necessary to useQCursor itself. I suppose creating theQCursor on the Qt side is what fixes the PySide issue.QApplication.restoreOverrideCursor() alone will not do anything if I don't move.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 pass1 Comment
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_functionI 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 pass2 Comments
function(*args, **kwargs), returningfunction's return value, and wrapping its call in atry...finally block for even more goodness. I can't seem to put that in this comment, so next I'll try editing your solution.return function(*args, **kwargs) might be useful as well, or was in my case at least.Better this way:
def waiting_effects(function): def new_function(*args, **kwargs): QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) try: return function(*args, **kwargs) except Exception as e: raise e print("Error {}".format(e.args[0])) finally: QApplication.restoreOverrideCursor() return new_functionComments
You can use the class QgsTemporaryCursorOverridehttps://qgis.org/api/classQgsTemporaryCursorOverride.html
But in Python, you should use the Python context manager which is now included in PyQGIS :
from qgis.PyQt.QtCore import Qtfrom qgis.utils import OverrideCursorwith OverrideCursor(Qt.WaitCursor): do_a_slow(operation)Comments
from qtpy import QtCore, QtGui, QtWidgetsdef waiting_effect(function): def waiting(*args, **kwargs): QtWidgets.QApplication.setOverrideCursor( QtCore.Qt.CursorShape(QtCore.Qt.CursorShape.WaitCursor) ) try: function(*args, **kwargs) finally: QtWidgets.QApplication.restoreOverrideCursor()If using PyQt5/PySide2, this might be of use, works nicely with decorating functions that have multiple arguments.
2 Comments
The best way to add the cursor according to me would be by using the decorators. By this way you can run any function by just adding the cursor to that function as decorator
import decorator@decorator.decoratordef showWaitCursor(func, *args, **kwargs): QtWidgets.QApplication.setOverrideCursor(QtCore.Qt.WaitCursor) try: return func(*args, **kwargs) finally: QtWidgets.QApplication.restoreOverrideCursor()@showWaitCursordef youFunc(): # Long processComments
I find I often need to do:
QApplication.setOverrideCursor(Qt.WaitCursor)QApplication.ProcessEvents()...QApplication.restoreOverrideCursor()However! During init of the GUI, I've found a modification needs to be made for some reason:
self.ui.show() # Or your equivalent code to show the widgetQApplication.processEvents()QApplication.setOverrideCursor(Qt.WaitCursor)app.processEvents()...QApplication.restoreOverrideCursor()Otherwise the cursor does not "restore" until the mouse passes over a widget that modifies it (like a QLineEdit), similar to what @Guimoute mentioned
Comments
Explore related questions
See similar questions with these tags.





