微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 只撤消最后一个。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。