![](/img/trans.png)
[英]How to set each item's selection color of QTableWidget in PyQt5
[英]PyQt5: How to set a custom mouse pointer for each role?
我正在用PyQt5为 GUI 创建一个Python 3.7应用程序。 我想在应用程序中自定义鼠标光标。
让我们从Qt5 中设置的标准光标开始,如下表所示: https : //doc.qt.io/qt-5/qt.html#CursorShape-enum 。 您会注意到 Qt5 有一个专用的 Enum Qt::CursorShape
描述相应游标的作用。 例如:
我想用定制的光标替换每个标准 Qt 光标:
起初我尝试过这样的事情:
pixmap = QPixmap("C:/../my_arrow.png")
cursor = QCursor(pixmap, 32, 32)
QApplication.setOverrideCursor(cursor)
不幸的是,这种方法不适合我的目的。 从文档:
应用程序覆盖光标旨在向用户显示应用程序处于特殊状态,例如在可能需要一些时间的操作期间。
覆盖光标将显示在所有应用程序的小部件中,直到restoreOverrideCursor()
或另一个setOverrideCursor()
。
换句话说,使用setOverrideCursor()
方法有两个缺点:
我必须手动跟踪鼠标指针应该改变的角色,每次调用setOverrideCursor()
并用正确的QCursor()
提供它。
我需要跟踪 Qt 自动调用restoreOverrideCursor()
任何地方,因为这有效地撤消了我自己的更改。 这将是一场与 Qt 的持久战。
我的第二种方法是使用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()
函数的工作方式。
你有最符合我目的的解决方案吗?
我从以下来源学到了很多东西:
不幸的是,他们都没有为我的问题提供解决方案。 我只是在这里提到它们,因为它们与我正在尝试做的事情有关 - 但与(!)不同。
# 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.