1. 介绍
我正在 中创建一个应用程序Python 3.7 与 PyQt5 对于 GUI。我想在应用程序中自定义鼠标光标。
让我们从 中设置的标准光标开始Qt5 如此处的表格所示:
https://doc.qt.io/qt-5/qt.html#CursorShape-enum .你会注意到 Qt5 有一个专用的 Enum Qt::CursorShape
描述相应游标的作用。例如:
我想更换每个标准 Qt 光标 与 定制 做了一个:
2. 第一种方法
起初我尝试过这样的事情:
pixmap = QPixmap("C:/../my_arrow.png")
cursor = QCursor(pixmap, 32, 32)
QApplication.setOverrideCursor(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.
The override cursor will be displayed in all the application's widgets untilrestoreOverrideCursor()
or anothersetOverrideCursor()
is called.
换句话说,使用
setOverrideCursor()
方法有两个缺点:setOverrideCursor()
每次都用适当的方式喂它 QCursor()
. restoreOverrideCursor()
,因为这有效地撤消了我自己的更改。这将是一场与 Qt 的持久战。 3. 第二种方法
我的第二种方法是玩弄
setCursor()
功能:pixmap = QPixmap("C:/../Arrow.png")
cursor = QCursor(pixmap, 32, 32)
my_widget.setCursor(cursor)
我在顶级小部件上执行此操作 -
QMainWindow()
- 使效果应用于整个应用程序。它工作得很好,但它有一个缺点。此函数仅更改“默认光标”(指向箭头),仅此而已。所有特殊游标仍然相同。
事实上,我想做这样的事情:
# Note: 'mainwin' is the QMainWindow().
mainwin.setCursor( QCursor(QPixmap("C:/../Arrow.png"), 32, 32), Qt.ArrowCursor )
mainwin.setCursor( QCursor(QPixmap("C:/../UpArrow.png"), 32, 32), Qt.UpArrowCursor )
mainwin.setCursor( QCursor(QPixmap("C:/../Cross.png"), 32, 32), Qt.CrossCursor )
mainwin.setCursor( QCursor(QPixmap("C:/../Wait.png"), 32, 32), Qt.WaitCursor )
mainwin.setCursor( QCursor(QPixmap("C:/../IBeam.png"), 32, 32), Qt.IBeamCursor )
mainwin.setCursor( QCursor(QPixmap("C:/../SizeVer.png"), 32, 32), Qt.SizeVerCursor )
mainwin.setCursor( QCursor(QPixmap("C:/../SizeHor.png"), 32, 32), Qt.SizeHorCursor )
mainwin.setCursor( QCursor(QPixmap("C:/../SizeBDiag.png"), 32, 32), Qt.SizeBDiagCursor )
mainwin.setCursor( QCursor(QPixmap("C:/../SizeFDiag.png"), 32, 32), Qt.SizeFDiagCursor )
...
不幸的是,
setCursor()
并非如此。功能有效。你有最符合我目的的解决方案吗?
4. 资源
我从以下来源学到了很多东西:
不幸的是,他们都没有为我的问题提供解决方案。我只是在这里提到它们,因为它们与我正在尝试做的事情有关 - 但与(!)不同。
最佳答案
# I'm coming```.
# 1. Set the cursor map
self.cursor_pix = QPixmap('exa.png')
# 2. Scale textures
self.cursor_scaled_pix = self.cursor_pix.scaled(QSize(20, 20), Qt.KeepAspectRatio)
# 3. Set cursor style and cursor focus position
self.current_cursor = QCursor(self.cursor_scaled_pix, -1, -1)
# 4. Set the cursor for the specified window
widget.setCursor(self.current_cursor)
关于python - PyQt5:如何为每个角色设置自定义鼠标指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56145023/