PyQt5:如何在多次覆盖后恢复默认游标?



是否有方法将鼠标光标图标行为重置为windows默认值?

当一个长时间的进程运行时,我想显示一个等待的鼠标光标图标,它完成了工作:

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

问题是,当在长过程中,我运行一个方法或事件或任何也调用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()

我尝试了这个,而不是restoreOverrideCursor():

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

但问题是它重写了箭头光标,示例窗口大小图标等的每个行为。

是否有办法恢复PyQt5中的默认鼠标行为?

要确保默认光标被重置,您可以这样做:

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

这是必要的,因为Qt维护一个覆盖游标的内部堆栈,而restoreOverrideCursor只撤销最后一个。

最新更新