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

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

在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 the cursor 
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值