How can I change the cursor shape with PyQt?

时间秒杀一切 提交于 2019-12-17 18:19:10

问题


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?


回答1:


I think QApplication.setOverrideCursor is what you're looking for:

PyQt5:

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication
...
QApplication.setOverrideCursor(Qt.WaitCursor)
# do lengthy process
QApplication.restoreOverrideCursor()

PyQt4:

from PyQt4.QtCore import Qt
from PyQt4.QtGui import QApplication
...
QApplication.setOverrideCursor(Qt.WaitCursor)
# do lengthy process
QApplication.restoreOverrideCursor()



回答2:


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:

from contextlib import contextmanager
from PyQt4 import QtCore
from PyQt4.QtGui import QApplication, QCursor

@contextmanager
def wait_cursor():
    try:
        QApplication.setOverrideCursor(QCursor(QtCore.Qt.WaitCursor))
        yield
    finally:
        QApplication.restoreOverrideCursor()

Then put the lengthy process code in a with block:

with wait_cursor():
    # do lengthy process
    pass



回答3:


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.

from PyQt4.QtCore import Qt
from PyQt4.QtGui import QApplication, QCursor, QMainWidget

def waiting_effects(function):
    def new_function(self):
        QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
        try:
            function(self)
        except Exception as e:
            raise e
            print("Error {}".format(e.args[0]))
        finally:
            QApplication.restoreOverrideCursor()
    return new_function

I can just put the decorator on any method I would like the spinner to be active on.

class MyWigdet(QMainWidget):

    # ...

    @waiting_effects
    def doLengthyProcess(self):
        # do lengthy process
        pass



回答4:


Better this way:

def waiting_effects(function):
    def new_function(*args, **kwargs):
        QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
        try:
            return function(*args, **kwargs)
        except Exception as e:
            raise e
            print("Error {}".format(e.args[0]))
        finally:
            QApplication.restoreOverrideCursor()
    return new_function



回答5:


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

import decorator
@decorator.decorator
def showWaitCursor(func, *args, **kwargs):
    QtWidgets.QApplication.setOverrideCursor(QtCore.Qt.WaitCursor)
    try:
        return func(*args, **kwargs)
    finally:
        QtWidgets.QApplication.restoreOverrideCursor()

@showWaitCursor
def youFunc():
    # Long process


来源:https://stackoverflow.com/questions/8218900/how-can-i-change-the-cursor-shape-with-pyqt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!