如何使用 PyQt 更改光标形状?
我有一个简单的应用程序,它运行的进程可能会持续几分钟才能完成。我试图向用户表明它正在处理请求 - 例如将光标更改为沙漏。
但我不能完全让它正常工作。我的所有尝试要么导致错误,要么没有效果。而且我似乎错误地调用了光标形状,因为 PyQt4.Qt.WaitCursor 返回模块不包含它的错误。
向用户指示进程正在运行的正确方法是什么?
I have a simple application that runs a process that can last for several minutes before completing. I am trying to provide an indication to the user that it is processing the request - such as changing the cursor to an hourglass.
But I cannot quite get it to work right. All of my attempts have resulted in either an error or had no effect. And I seem to be calling the cursor shapes incorrectly, since PyQt4.Qt.WaitCursor
returns an error that the module does not contain it.
What is the correct way to indicate to the user that the process is running?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我认为 QApplication.setOverrideCursor 就是您正在寻找的:
<强>PyQt6:
PyQt5:
PyQt4:
I think QApplication.setOverrideCursor is what you're looking for:
PyQt6:
PyQt5:
PyQt4:
虽然卡梅伦和大卫的答案非常适合在整个函数上设置等待光标,但我发现上下文管理器最适合为代码片段设置等待光标:
然后将冗长的流程代码放入 with 块中:
While Cameron's and David's answers are great for setting the wait cursor over an entire function, I find that a context manager works best for setting the wait cursor for snippets of code:
Then put the lengthy process code in a with block:
ekhumoro的解决方案是正确的。该解决方案是为了风格而进行的修改。我使用了 ekhumor 所做的,但使用了 python 装饰器。
我可以将装饰器放在我希望微调器处于活动状态的任何方法上。
ekhumoro's solution is correct. This solution is a modification for the sake of style. I used what ekhumor's did but used a python decorator.
I can just put the decorator on any method I would like the spinner to be active on.
这样更好:
Better this way:
您可以使用类 QgsTemporaryCursorOverride https://qgis.org/api/classQgsTemporaryCursorOverride.html
但是在Python中,您应该使用Python上下文管理器,它现在包含在PyQGIS中:
You can use the class QgsTemporaryCursorOverride https://qgis.org/api/classQgsTemporaryCursorOverride.html
But in Python, you should use the Python context manager which is now included in PyQGIS :
如果使用 PyQt5/PySide2,这可能有用,可以很好地处理具有多个参数的装饰函数。
If using PyQt5/PySide2, this might be of use, works nicely with decorating functions that have multiple arguments.
根据我的说法,添加光标的最佳方法是使用装饰器。通过这种方式,您只需将光标添加到该函数作为装饰器即可运行任何函数
The best way to add the cursor according to me would be by using the decorators. By this way you can run any function by just adding the cursor to that function as decorator
我发现我经常需要做的是:
但是!在 GUI 初始化期间,我发现由于某种原因需要进行修改:
否则光标不会“恢复”,直到鼠标经过修改它的小部件(如 QLineEdit),类似于 @Guimoute 提到的
I find I often need to do:
However! During init of the GUI, I've found a modification needs to be made for some reason:
Otherwise the cursor does not "restore" until the mouse passes over a widget that modifies it (like a QLineEdit), similar to what @Guimoute mentioned