Qt Mouse Pointer Rotation
i am using Qt5.5 on an embedded linux and depending on the position of the screen i am rotating my QML application.
The probem i am facing is to rotate the mouse pointer too.
Once its 180° rotated, the mouse pointer uses its default coordinates like on 0° orientation.
Is there any chance to rotate the pointer ?
@michaelL OK, I haven't tried this (and I assume you REALLY want to do this!) but here's my hypothesis
QCursor getCursor()on your main window
- Get the Pixmap from that Cursor
- Now create a rotated pixmap like this…
QPixmap rotated_pixmap = cursor.getPixmap().transformed(QTransform().rotate(180));
- Then set the cursor again QCursor newCursor(rotated_cursor)
You should vertically mirror the hot spot too.
WARNING! Your system may only support mask/bitmap cursors in which case the above will work but with mods to use QBitmap and QCursor::mask and QCursor::bitmap.
Good luck. Never tried this! Interested to see if it works.
@michaelL OK, I tried it on my Mac and it works...
Here's the code for DL http://kuiash.net/dokuwiki/doku.php?id=flip_mouse_cursor
@matthew-kuiash My application is running in QML, i think this could work for the cursor icon but what happens to its movement?
When i move the mouse pointer downwards, it goes upwards and so on. I have to invert the mouse cursor coordinates too.
If i do this the whole time, my one core embedded CPU will be overloaded with calculation and the application will be very slow i think.
I dont know if there is a simple way on doing this directly on linux or if there is a Qt Mouse event input handler so it does invert it on every input event for me.
@michaelL Dude! Pain! Well, these kind of problems are typically solved at driver or window manager level not in applications. You could just take complete control I guess and do all the work yourself in your app. I wouldn't say it would take too much time as I used to have to do the same on 8Mhz 286 machines. But yes, it'll take time.
If you're using X there seem to be a solution. I found an Ubuntu thread about inverting mouse axis here...
If you're not using X, well, there are several places you could invert. Application, Qt Platform plugin (that is produce you own platform library and put this functionality in there or down in the OS drivers.
My screen rotates only to 180° and normal orientation is 0°.
I could make two custom mouse pointer shapes. One is the inverse from the other.
By default orientation the application doesnt do anything. By 180° orientation i change the mouse pointer shape and change the QCursor position.
With QCursor:pos() i get a QPointer back and i simply multiply x*-1 and y*-1 and set this new QPointer on the QCursor:setPos().
The mouse pointer navigation should be inverted too.
I will try this later on.
with x*-1 y*-1 it wont work. I have to change to my display coordinates. So the screen zeros (0,0) have to be the screen resolution (displya.width, display.height). So the moving cursor will be like QPointer(display.width - x, display.height -y), where x and y are the new position coordinates from the mouse pointer.
@matthew-kuiash So i can change the cursor shape and i can change its coordinates.
When i move the mouse it goes to where it should be but it appears in its original location too.
What i mean is that when the mouse event occurs its too late to change its coordinates by then, because the mouse cursor will be displayed
first on the screen and jumps to the movement event.
In main i installed an event to detect when the mouse is moving and change its coordinate:
if (event->type() == QEvent::MouseMove)
QMouseEvent mouseEvent = static_cast<QMouseEvent>(event);
mouse_cursor.set_cursor_position(1280-mouseEvent->pos().x(), 800 -mouseEvent->pos().y());
I also create an Item in QML and had a mous cursor picture in it. It looked and moved like a mouse but it was only an image.
I can set the mouse cursor shape to blank so its not displayed and than have my image to mouse cursor to replace him.
But how do i disable the mouse to click the buttons, even if its not visible?
My QML Item is a Rectangle with an Image in it. How do i make it to click buttons or to be able to interact to MouseAreas?
I found some interresting documentation:
QT_QPA_EGLFS_ROTATION - Specifies the rotation applied to software-rendered content in QWidget-based applications. Supported values are 180, 90, and -90. This does not apply to OpenGL-based windows, including Qt Quick. Qt Quick applications can apply transformations in their QML scene instead. The standard eglfs mouse cursor always takes the value into account, with appropriately positioned and rotated pointer image, regardless of the application type. The special cursor implementations, such as the KMS/DRM backend's hardware cursor, may not support rotation.
But why is my mouse also not rotating?
You've quite possibly run into this bug, supposedly fixed in Qt 5.8: https://bugreports.qt.io/browse/QTBUG-39959
We're using QML, and our display is installed in our hardware upside down. I've successully flipped by ts_lib touch sense and my mouse cursor/mouse movement using the following:
Touch sense works fine, mouse cursor looks fine, moves fine. But mouse clicks use their "unrotated" position ... For example, clicking the the top-right corner of the UI registers as a click in the bottom-left corner. Working on figuring this out now...
@Steve172 thanks for your answer. I am still working on Qt5.7.1 but i will upgrade to 5.8 soon and will give it a try.
My touch works fine but only mouse rotation is the issue.