Tobias Sch.
Tobias Sch.

Reputation: 97

How to replace all cursor appearances within a QML program?

I'm working on a game that uses QML for its UI.

I would like to replace all cursors appearances with cursor-images that are more fitting to the game style (e.g. pointing skeleton hand instead of the normal Qt::ArrowCursor).

Calling QGuiApplication::setOverrideCursor() seams not to be a practical solution as I can not "overwrite" each MouseArea to call may replaceCursor() magic-global-function. For example the change column with cursor within a TableView is currently impossible for me to manipulate.

To me the most practical solution would be to replace the appearance of all cursors but leaf Qt with the tasks to correctly choose the cursor style.

Thanks for any help!

Upvotes: 1

Views: 985

Answers (1)

dtech
dtech

Reputation: 49289

You can still use QGuiApplication::setOverrideCursor() to decorate your mouse areas. It works like a stack, you can set and then restore cursors, so you begin with setting an initial cursor from main.cpp, and then you use an "overloaded" MouseArea which sets its cursor using setOverrideCursor() as well, instead of using the QML functionality.

For example:

onContainsMouseChanged: {
  if (containsMouse) Sys.setOverrideCursor(yourCursortype)
  else Sys.restoreOverrideCursor()
}

Of course, that means you will have to create an auxiliary object that will call those functions from C++, and expose it to QML so it can be called from there.

Upvotes: 2

Related Questions