1

How to set default system mouse icons in Qt applications?

From what I know Qt has a special set of cursor icons (that are not the same ones with the cursors that come with the operating system or the cursor theme).

...
<button style="cursor: pointer;">Test mouse cursor</button>
...

Taking hand mouse icon as example:

Default hand cursor set at operating system level.


Qt cursor - that is not the same with the operating system cursor.

I want to use the mouse icons theme that is set at operating system level, instead of using Qt cursor theme.

How can I do this?

Community
  • 1
  • 1
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
  • When exactly do you want the different mouse cursor to appear? Above a certain widget? Above the entire application window? Even outside the application window? – Silicomancer Sep 17 '14 at 08:00
  • @Silicomancer In the entire application. I want to use normal hand icon (that is configured at operating system level), not the one from Qt environment. – Ionică Bizău Sep 17 '14 at 09:53
  • Only an idea, never used this before... did you try QWidget::setCursor() on the main window? – Silicomancer Sep 17 '14 at 09:59
  • @Silicomancer I think I was not clear enough. Setting the cursor is not a problem, but the cursor theme is the issue. See the edit. – Ionică Bizău Sep 17 '14 at 12:34
  • @IonicăBizău so if you want another cursor, then you can download cursor(picture) which you need and install it as a pixmap, as I wrote in my answer. – Jablonski Sep 17 '14 at 12:55
  • @Chernobyl Hmm... That's also not a good idea because it will be hard-coded. I want to use the *hand* cursor that is set by the active cursor theme, not setting it in my app. I don't know why Qt changes it... – Ionică Bizău Sep 17 '14 at 12:58
  • @IonicăBizău http://qt-project.org/doc/qt-4.8/qt.html#CursorShape-enum Here is list of all embedded cursors in Qt. If something shows not properly, then OS change it or something else. And if you want change it you should set small pixmap as cursor and your cursor will be same on different platforms. Pixmap you can embedded in exe, it calls Qt resourse system. Search picture with alpha-channel, in this case your cursor will be natural and beautiful – Jablonski Sep 17 '14 at 13:06
  • @Chernobyl I don't understand if I'm not clear enough or this is not possible: I want to use system default mouse theme on each platform (not same mouse icons on all platforms, but default ones on each platform). Is this possible? – Ionică Bizău Sep 17 '14 at 13:38
  • @IonicăBizău I don't know why you can't get normal cursor. On my windows I get system cursor when use `Qt::PointingHandCursor`, it works perfectly.Maybe you have specific Linux distribution, I don't know. So I see 2 solutions. 1. Wait for help here. 2. Use `Qt::PointingHandCursor` on windows(maybe linux) etc, but set pixmap of needed cursor as cursor in your OS(there are examples of standard cursor in web). But I agree with you that it is really not so good. – Jablonski Sep 17 '14 at 13:54

2 Answers2

0

You can use setOverrideCursor to change the cursor in the entire application. Just call it in main or in the constructor of your MainWindow :

qApp->setOverrideCursor(QCursor(Qt::PointingHandCursor));
Nejat
  • 31,784
  • 12
  • 106
  • 138
0

But setOverrideCursor() has one disadvantage. As documentation said:

Sets the application override cursor to cursor.

Application override cursors are intended for showing the user that the application is in a special state, for example during an operation that might take some time.

This cursor will be displayed in all the application's widgets until restoreOverrideCursor() or another setOverrideCursor() is called.

Application cursors are stored on an internal stack. setOverrideCursor() pushes the cursor onto the stack, and restoreOverrideCursor() pops the active cursor off the stack. changeOverrideCursor() changes the curently active application override cursor.

Every setOverrideCursor() must eventually be followed by a corresponding restoreOverrideCursor(), otherwise the stack will never be emptied.

Link: http://qt-project.org/doc/qt-4.8/qapplication.html#setOverrideCursor

It means that all widgets will have this cursor and you can't change it. So I have next solution:

Set cursor to your mainwindow, it will be default cursor, but you be able to change cursor of every widget you want, but mainWindow's cursor will be default.

For example:

this->setCursor(QCursor(Qt::PointingHandCursor));//it is default cursor
//qApp->setOverrideCursor(QCursor(Qt::PointingHandCursor));

QPixmap pix("path");
QCursor cur(pix);
ui->textEdit->viewport()->setCursor(cur);//when we hover the textEdit we get this pixmap as cursor.
Jablonski
  • 18,083
  • 2
  • 46
  • 47