Eqzt111
Eqzt111

Reputation: 590

PyQt5: How to restore the default cursor after multiple overrides?

Is there a method to reset the mouse cursor icon behaviour to windows default?

When a long process running I'd like to show a waiting mouse cursor icon, and it gets the job done:

# Set the mouse cursor to wait cursor
QtWidgets.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.WaitCursor))

# Long process start
# Long process end

#reset mouse cursor to default behaviour
QtWidgets.QApplication.restoreOverrideCursor()

The problem is when in the long process I run a method or event or whatever that also calls the setOverrideCursor:

# Set the mouse cursor to wait cursor
QtWidgets.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.WaitCursor))

# Long process start
    # In the long process theres a method or event or whatever that calls again the wait cursor
    # and wait cursor becomes the overwritten mouse cursor
    QtWidgets.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.WaitCursor))
    # Some code running...
    QtWidgets.QApplication.restoreOverrideCursor()
# Long process end

# the restoreOverrideCursor() restores the wait cursor instead of the default mouse cursor behaviour 
# and the mouse icon just spinning forever
QtWidgets.QApplication.restoreOverrideCursor()

I tried this, instead of restoreOverrideCursor():

QtWidget.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))

but the problem it owerwrites every behaviour with arrow cursor, example window resize icon and so on.

Is there a way to restore the default mouse behaviour in PyQt5?

Upvotes: 2

Views: 1386

Answers (1)

ekhumoro
ekhumoro

Reputation: 120608

To ensure the default cursor is reset, you can do this:

while QApplication.overrideCursor() is not None:
    QApplication.restoreOverrideCursor()

This is needed because Qt maintains an internal stack of override cursors, and restoreOverrideCursor only undoes the last one.

Upvotes: 4

Related Questions