*QT QGraphicsScene、QGraphicsItem、QGraphicsProxyWidget、QWidget间的事件传递

QT QGraphicsScene、QGraphicsItem、QGraphicsProxyWidget、QWidget间的事件传递

在项目开发中由于没有能够深刻理解QT QgraphicsScene场景中的事件传递的原理,导致浪费了两天时间。下面就将这两天对QT时间QT QgraphicsScene场景中的事件传递的理解分享一下。

这里写图片描述
QGraphicsScene为自定义画图场景,通过addItem()可以将自定义item放入scene场景,也可以通过QGraphicsProxyWidget代理类将QWidget封装为item中,由于图形场景层次叠加,势必导致鼠标事件被劫持。因此本位详细说明不同鼠标事件在不同层次间的传递方法。

鼠标单击事件:
void  QGraphicsProxyWidget::mousePressEvent(QGraphicsSceneMouseEvent *event);   声明1
如果QWidget想接收鼠标点击事件,在鼠标点击事件中增加事件传递函数
QGraphicsProxyWidget::mousePressEvent(event);                               函数1
而在重写QWidget的mousePressEvent事件时需要用函数2才能将事件传递到QWidget控件中
QWidget::mousePressEvent(event);                                            函数2
只有这样鼠标单击事件才能被传递至QWidget场景中。

鼠标移动事件:
void QGraphicsProxyWidget::mouseMoveEvent(QGraphicsSceneMouseEvent *event)      声明2
QWidget被封装为QGraphicsItem后想实现移动目的,如果重写声明2函数实现移动,则需要在时间函数中增加
QGraphicsItem::mouseMoveEvent(event);                                       函数3
因为在场景内只有item能够实现移动。同样QWidget向下传递时间使用QWidget::mouseMoveEvent(event)

鼠标释放事件:
void QGraphicsProxyWidget::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)   声明3
QPushButton控件是通过QWidget::mouseReleaseEvent事件来触发。
mouseReleaseEvent事件传递类似于mousePressEvent事件
  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值