关于子类化QGraphicsTextItem只有很小一部分区域能够获取事件的问题

在QT图形视图框架下,子类化QGraphicsTextItem并设置交互标志后,发现只有小部分区域能捕获事件。问题在于QGraphicsTextItem未使用重写的boundingRect()。解决方案是不仅要设置painter的字体,还需设置QGraphicsTextItem的font和plainText,以确保正确计算事件区域。

在QT图形视图框架中,想要Item响应获得焦点事件,首先需要设置本Item可以获得焦点
一般Item可以使用

setFlag(QGraphicsItem::ItemIsFocusable);

来使Item可以获取焦点事件,但是针对QGraphicsTextItem,它有一个另外的设置方法,首先来看这个方法的函数原型

void setTextInteractionFlags(Qt::TextInteractionFlags flags)

帮助文档中对这个方法是这样解释的:
Sets the flags flags to specify how the text item should react to user input.

The default for a QGraphicsTextItem is Qt::NoTextInteraction. This function also affects the ItemIsFocusable QGraphicsItem flag by setting it if flags is different from Qt::NoTextInteraction and clearing it otherwise.

By default, the text is read-only. To transform the item into an editor, set the Qt::TextEditable flag.

这样就比较清晰了,参数flags代表了QGraphicsTextItem的一些属性,
再来看看Qt::TextInteractionFlags

This enum specifies how a text displaying widget reacts to user input.

Constant Value Description
Qt::NoTextInteraction           0   No interaction with the text is possible.
Qt::TextSelectableByMouse       1   Text can be selected with the mouse and copied to the clipboard using a context menu or standard keyboard shortcuts.
Qt::TextSelectableByKeyboard    2   Text can be selected with 
`QGraphicsTextItem` 子类无法接收鼠标双击事件,可从以下方面解决。 ### 重写 `mouseDoubleClickEvent` 方法 在子类中重写 `mouseDoubleClickEvent` 方法,确保该方法能正确处理鼠标双击事件。 ```python from PyQt5.QtWidgets import QGraphicsTextItem from PyQt5.QtCore import Qt class CustomTextItem(QGraphicsTextItem): def mouseDoubleClickEvent(self, event): # 处理鼠标双击事件 print("Double clicked!") # 调用父类的方法以维持其他默认行为 super().mouseDoubleClickEvent(event) ``` ### 确保图元可交互 要保证图元能够接收鼠标事件,需要设置图元的标志位。在子类的构造函数中,使用 `setFlags` 方法开启相关标志。 ```python class CustomTextItem(QGraphicsTextItem): def __init__(self, parent=None): super().__init__(parent) # 开启可选择、可移动和可交互标志 self.setFlags(self.flags() | QGraphicsTextItem.ItemIsSelectable | QGraphicsTextItem.ItemIsMovable) def mouseDoubleClickEvent(self, event): print("Double clicked!") super().mouseDoubleClickEvent(event) ``` ### 检查事件传播 要保证事件能正确传播到图元。`QGraphicsScene` 负责传播事件到每个图元,确保场景没有拦截或处理掉鼠标双击事件。 ```python from PyQt5.QtWidgets import QGraphicsScene, QGraphicsView, QApplication import sys app = QApplication(sys.argv) scene = QGraphicsScene() custom_item = CustomTextItem() scene.addItem(custom_item) view = QGraphicsView(scene) view.show() sys.exit(app.exec_()) ``` ### 启用鼠标跟踪(可选) 若图元需要实时跟踪鼠标位置,可启用鼠标跟踪。 ```python class CustomTextItem(QGraphicsTextItem): def __init__(self, parent=None): super().__init__(parent) self.setFlags(self.flags() | QGraphicsTextItem.ItemIsSelectable | QGraphicsTextItem.ItemIsMovable) # 启用鼠标跟踪 self.setAcceptHoverEvents(True) def mouseDoubleClickEvent(self, event): print("Double clicked!") super().mouseDoubleClickEvent(event) ``` ### 检查场景和视图设置 确保 `QGraphicsScene` 和 `QGraphicsView` 没有设置可能影响事件传播的属性。比如,确保视图没有拦截鼠标事件
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值