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

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

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

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

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

但它的问题是用箭头光标写入每个行为,例如 window 调整图标大小等等。

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

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

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

这是必需的,因为 Qt 维护一个覆盖游标的内部堆栈,并且 restoreOverrideCursor 只撤消最后一个。