How to create a cursor theme for an entire Qt application?



  • Hi all,

    I want to achieve a full cursor theme for my application, but I'm stuck. If the cursor is over a QLineEdit I want my own IBeamCursor, if it's over a QSplitterHandle I want my own SizeHorCursor.

    Currently I know about QWidget::setCursor() and QGuiApplication::setOverrideCursor() but this doesn't helps at all.

    I've tried to create an event filter and install it to QApplication, but this doesn't help, because I need to detect QCursor::shape which isn't possible because I can't get a handle to any instance of QCursor.

    Next step was to install the event filter on QMainWindow and get the QWidget::cursor() instance from there to handle it, but this failed too, because it doesn't change it's type belonging to the underlaying widget for example to IBeamCursor on a QLineEdit.

    Next step was to install the event filter on the underlaying widgets itself, such as QLineEdit, QTextEdit, etc, too. This finally worked and I was able to change the cursor to the right shape. Nevertheless it doesn't work on combined widgets like QSpinBox (Arrow/IBeam), QSplitter(Arrow/SizeHor), etc.

    Can it be that hard to set a full cursor theme for an entire Qt Application without installing filters to every widget? I mean we are talking about Qt 5.9.3! .. or am I off the track?

    Kind regards,
    Mike


  • Moderators

    @Lachrymology said in How to create a cursor theme for an entire Qt application?:

    I've tried to create an event filter and install it to QApplication, but this doesn't help, because I need to detect QCursor::shape which isn't possible because I can't get a handle to any instance of QCursor.

    sine you installed an event-filter you also receive the watched QObject which you can cast to a QWidget and access it's cursor.
    But depending on your application structure this might end up in bad performance.

    The best way would be subclass all your widgets where you want a different cursor shape in your application.



  • Yeah, that's what I'm guessing, that it would end up in bad performance and the other way around to subcall all used widgets would be hell of a work.

    Another idea would be QStyle or QStyleSheet .. are they capable to solve my issue in any way?



  • @Lachrymology
    I imagen you should be able to subclass QCursor and overwrite the default constructor of QCursor(Qt::CursorShape shape) with one that uses your own Bitmaps?

    I haven't done that myself yet, so just an idea.


  • Moderators

    @J.Hilk
    QCursor is already capable of displaying custom bitmaps/pixmaps.
    The problem here is how to quickly set the custom cursors for the whole application.



  • @raven-worx said in How to create a cursor theme for an entire Qt application?:

    @J.Hilk
    QCursor is already capable of displaying custom bitmaps/pixmaps.
    The problem here is how to quickly set the custom cursors for the whole application.

    I read that from the docu, the issue, from what I understand, is that the op does not want to manually change the cursor via subclassing widgets.

    To set a cursor shape use QCursor::setShape() or use the QCursor constructor which takes the shape as argument, or you can use one of the predefined cursors defined in the Qt::CursorShape enum.
    

    Out of this I read that the standart curser, when he 'enters' a widgets is set via QCursor constructor and QCursorShape-enum.

    The idea was to hock into the standart constructor (the one via QCursorShape-enum), switch-case over the enum. Than either use call the base function or use the custom constructor for shapes the op has custom bitmaps for.


  • Moderators

    @J.Hilk said in How to create a cursor theme for an entire Qt application?:

    The idea was to hock into the standart constructor (the one via QCursorShape-enum), switch-case over the enum. Than either use call the base function or use the custom constructor for shapes the op has custom bitmaps for.

    this only works by modifying the Qt source code.
    Despite the possible (i don't know if there will be) side effects.



  • @raven-worx are you sure? I assumed one could replace the default cursor with QGuiApplication::setOverrideCursor()

    Edit: looking a bit deeper into QGuiApplication::setOverrideCursor(), it seems I was mistaken and one would have indeed have to replace the default Qt.Cursor via Sourcecode change.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.