python - PyQt5:如何为每个角色设置自定义鼠标指针?

标签 python qt pyqt qt5 pyqt5

1. 介绍

我正在 中创建一个应用程序Python 3.7 PyQt5 对于 GUI。我想在应用程序中自定义鼠标光标。

让我们从 中设置的标准光标开始Qt5 如此处的表格所示:
https://doc.qt.io/qt-5/qt.html#CursorShape-enum .你会注意到 Qt5 有一个专用的 Enum Qt::CursorShape描述相应游标的作用。例如:

Qt standard cursors



我想更换每个标准 Qt 光标 定制 做了一个:

Custom cursors



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 until restoreOverrideCursor() or another setOverrideCursor() is called.



换句话说,使用 setOverrideCursor()方法有两个缺点:
  • 我必须手动跟踪鼠标指针应更改为哪个角色,请调用 setOverrideCursor()每次都用适当的方式喂它 QCursor() .
  • 我需要跟踪 Qt 自动调用的任何地方 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. 资源

    我从以下来源学到了很多东西:
  • Default mouse icons for Qt applications
  • Is there a way to create a custom animated / gif QCursor?

  • 不幸的是,他们都没有为我的问题提供解决方案。我只是在这里提到它们,因为它们与我正在尝试做的事情有关 - 但与(!)不同。

    最佳答案

    # 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/

    相关文章:

    qt - 如何访问在 QtCreator Designer 中创建的 QLabel?

    python - 使用 python 中的 PyQt5 按下按钮时更改 QML 中的标签

    python - 在pyqt qtablewidget中排序

    python - numpy.unique 对 numpy.array 的对象表现得很奇怪

    python - 如何将 BeautifulSoup HREF 搜索从 <a> 扩展到 <td>

    qt - 在播放视频的 QWidget 之上包含 QML 的 QDeclarativeView 的透明度(使用声子或 libvlc)

    python - QTreeWidget - 从排序中排除顶级项目

    python - 稀疏 hstack 和奇怪的 dtype 转换错误

    javascript - 将 python 代码移植到 javascript

    qt - 如何在Qthread中使用QAndroidJniEnvironment指针?