PyQt5:如何为每个角色设置自定义鼠标指针? [英] PyQt5: How to set a custom mouse pointer for each role?

查看:78
本文介绍了PyQt5:如何为每个角色设置自定义鼠标指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1.介绍

我正在 Python 3.7 中创建一个应用程序,使用 PyQt5 作为 GUI.我想在应用程序中自定义鼠标光标.

让我们从 Qt5 中设置的标准光标开始,如下表所示:

 
我想用一个自定义制作的光标替换每个标准 Qt 光标:

<块引用>

 

2.第一种方法

起初我尝试过这样的事情:

pixmap = QPixmap("C:/../my_arrow.png")光标 = QCursor(像素图, 32, 32)QApplication.setOverrideCursor(游标)

不幸的是,这种方法不适合我的目的.来自文档:

<块引用>

应用程序覆盖光标旨在向用户显示应用程序处于特殊状态,例如在可能需要一些时间的操作期间.
 
覆盖光标将显示在所有应用程序的小部件中,直到 restoreOverrideCursor() 或另一个 setOverrideCursor() 被调用.

换句话说,使用 setOverrideCursor() 方法有两个缺点:

  1. 我必须手动跟踪鼠标指针应该更改为哪个角色,每次调用 setOverrideCursor() 并使用正确的 QCursor().

  2. 我需要跟踪 Qt 自动调用 restoreOverrideCursor() 的任何地方,因为这会有效地撤消我自己的更改.这将是一场与 Qt 的持久战.

 

3.第二种方法

我的第二种方法是使用 setCursor() 函数:

pixmap = QPixmap("C:/../Arrow.png")光标 = QCursor(像素图, 32, 32)my_widget.setCursor(光标)

我在顶层小部件上执行此操作 - QMainWindow() - 这样效果就会应用于整个应用程序.

效果很好,但有一个缺点.此函数仅更改默认光标"(指向箭头),仅此而已.所有特殊游标仍然相同.

 
事实上,我想做这样的事情:

# 注意:'mainwin' 是 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.资源

我从以下来源学到了很多东西:

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

解决方案

# 我来了```.# 1.设置光标映射self.cursor_pix = QPixmap('exa.png')# 2. 缩放纹理self.cursor_scaled_pix = self.cursor_pix.scaled(QSize(20, 20), Qt.KeepAspectRatio)# 3.设置光标样式和光标焦点位置self.current_cursor = QCursor(self.cursor_scaled_pix, -1, -1)# 4. 为指定窗口设置光标widget.setCursor(self.current_cursor)

1. Intro

I'm creating an application in Python 3.7 with PyQt5 for the GUI. I would like to customize the mouse cursors in the application.

Let's start from the standard cursor set in Qt5 as shown in a table here: https://doc.qt.io/qt-5/qt.html#CursorShape-enum. You will notice that Qt5 has a dedicated Enum Qt::CursorShape describing the role of the corresponding cursor. For example:

 
I would like to replace each Standard Qt cursor with a custom made one:

 

2. First approach

At first I tried something like this:

pixmap = QPixmap("C:/../my_arrow.png")
cursor = QCursor(pixmap, 32, 32)
QApplication.setOverrideCursor(cursor)

Unfortunately, this approach is not good for my purpose. From the documentation:

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.

In other words, using the setOverrideCursor() approach has two downsides:

  1. I would have to track manually to which role the mouse pointer should change, call setOverrideCursor() each time and feed it with the proper QCursor().

  2. I'd need to track wherever Qt automatically calls restoreOverrideCursor(), because that effectively undoes my own changes. It would be a constant battle against Qt.

 

3. Second approach

My second approach was playing around with the setCursor() function:

pixmap = QPixmap("C:/../Arrow.png")
cursor = QCursor(pixmap, 32, 32)
my_widget.setCursor(cursor)

I do this on the toplevel widget - the QMainWindow() - such that the effect is applied on the whole application.

It works great, but it has one downside. This function only changes the "default cursor" (the pointing arrow) but that's it. All the special cursors are still the same.

 
In fact, I would like to do something like this:

# 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 )
...

Unfortunately, that's not how the setCursor() function works.

 
Do you have a solution that matches best my purpose?

 

4. Resources

I've learned a lot from the following sources:

Unfortunately, none of them provided the solution to my problem. I just mention them here, because they are related to - but not the same as(!) - what I'm trying to do.

解决方案

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

这篇关于PyQt5:如何为每个角色设置自定义鼠标指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆