简体   繁体   中英

How to change the cursor in Qt C++

How do I change the cursor to an image I have in the local machine? I have followed this tutorial for code reference: http://www.newthinktank.com/2018/07/qt-tutorial-5-paint-app/

Try this in your .qrc file:

<RCC>
  <qresource prefix="/">
    <file>cursors/my_cursor.png</file>
  </qresource>
</RCC>

Then program this way:

QPixmap p = QPixmap(":my_cursor");
QCursor c = QCursor(p, 0, 0);
setCursor(c);

I think you should use the class QCursor , once the mouse is inside the image you can modify its shape by using the function setShape().

Like the documentation says:

To associate a cursor with a widget, use QWidget::setCursor(). To associate a cursor with all widgets (normally for a short period of time), use QGuiApplication::setOverrideCursor().

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.

You can change cursor temprorarily and on a stable basis.

Stable way means, the cursor is just set as it is. Use QWidget::setCursor() . Note, the object, where this is applied has to be a QWidget.

Temporary way overrides the permanent cursor. When you do QGuiApplication::setOverrideCursor(QCursor(/* your cursor here */)); you add this cursor to the stack, atop. If several cursors are laid, while popping them, we get them in reverse order. To pop a cursor we do: QGuiApplication::restoreOverrideCursor();

It doesn't really matter for just a single change or a swing to and fro, but this will matter if there's a chance that cursors pile.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM