Default mouse icons for Qt applications

微笑、不失礼 提交于 2019-12-08 05:16:57

问题


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?


回答1:


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));



回答2:


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.


来源:https://stackoverflow.com/questions/25883457/default-mouse-icons-for-qt-applications

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!