nucleic / enaml

Declarative User Interfaces for Python
http://enaml.readthedocs.io/en/latest/
Other
1.53k stars 130 forks source link

[DOC] How to change mouse cursor to "busy" state and back ? #509

Open Kochise opened 1 year ago

Kochise commented 1 year ago

This there a wait to do this "natively" from within Enaml :

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

MatthieuDartiailh commented 1 year ago

No there is currently no native way to do this. I remember looking into it at one point but never spun up a working solution.

Kochise commented 1 year ago

O-kayyy...

atari_gem_bee

Kochise commented 1 year ago

https://www.riverbankcomputing.com/static/Docs/PyQt5/api/qtwidgets/qwidget.html#setCursor https://www.riverbankcomputing.com/static/Docs/PyQt5/api/qtcore/qt.html#CursorShape https://kb.froglogic.com/squish/qt/howto/verifying-mouse-cursor/

bburan commented 1 year ago

@Kochise There's currently no way to do this via Enaml. You're welcome to submit a PR implementing this.

Kochise commented 1 year ago

Will see what I can do. Thank for the tip.

pjcunningham commented 1 year ago

Dirty hack (I'm using PySide2):

from enaml.qt.qt_application import QtApplication
from PyQt5.QtGui import QCursor
from enaml.qt import QtCore

class CustomQtApplication(QtApplication):

    def set_wait_cursor(self):
        app = self._qapp
        app.setOverrideCursor(QCursor(QtCore.Qt.WaitCursor))

    def restore_cursor(self):
        app = self._qapp
        app.restoreOverrideCursor()

# redefine deferred_call to use CustomQtApplication
def deferred_call(callback, *args, **kwargs):
    app = CustomQtApplication.instance()
    if app is None:
        raise RuntimeError('Custom Application instance does not exist')
    app.deferred_call(callback, *args, **kwargs)

Example use in thread, referencing this Gist, Simple threads with Enaml

def worker(application: CustomQtApplication, application_model: ApplicationModel, progress_model: ProgressModel):
    deferred_call(application.set_wait_cursor)
    p = 0
    while p <= 100:
        deferred_call(setattr, progress_model, 'value', p)
        deferred_call(application_model.update_log, [f"Incrementing Progress Bar with value:{str(p)}"])
        p += 1
        sleep(0.2)
    deferred_call(setattr, progress_model, 'busy', False)
    deferred_call(application.restore_cursor)