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

标签 python qt pyqt5 reset mouse-cursor

有没有办法将鼠标光标图标行为重置为 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只撤消最后一个。

关于python - PyQt5:如何在多次覆盖后恢复默认光标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66185831/

相关文章:

python - 在同一 qtchart 上绘制烛台和 5 天平均线,但给出两个 x 轴图

python - MySQL 行列值格式到 python 字典

python - pylint人类可读消息ID列表?

python - 如何从另一个线程更新 GUI?使用Python

python - 如何创建一个已知的主机文件?

c++ - 如何从主窗口访问另一个Windows ui?

c++ - 使用外部库推送 QT Project GIT

python - 使用 QtWebChannel 的 folium 弹出窗口

c++ - 需要用 QFileDialog 保存一个新文件

python - PyQt5 QMainWindow,QDockWidget,适合屏幕尺寸自动调整