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

来源:互联网 发布:mac双系统 没了一个 编辑:程序博客网 时间:2024/06/01 07:42

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

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

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

鼠标单击事件:
void  QGraphicsProxyWidget::mousePressEvent(QGraphicsSceneMouseEvent *event);   声明1
  • 1
  • 1
如果QWidget想接收鼠标点击事件,在鼠标点击事件中增加事件传递函数
QGraphicsProxyWidget::mousePressEvent(event);                               函数1
  • 1
  • 1
而在重写QWidget的mousePressEvent事件时需要用函数2才能将事件传递到QWidget控件中
QWidget::mousePressEvent(event);                                            函数2
  • 1
  • 1
只有这样鼠标单击事件才能被传递至QWidget场景中。鼠标移动事件:
void QGraphicsProxyWidget::mouseMoveEvent(QGraphicsSceneMouseEvent *event)      声明2
  • 1
  • 1
QWidget被封装为QGraphicsItem后想实现移动目的,如果重写声明2函数实现移动,则需要在时间函数中增加
QGraphicsItem::mouseMoveEvent(event);                                       函数3
  • 1
  • 1
因为在场景内只有item能够实现移动。同样QWidget向下传递时间使用QWidget::mouseMoveEvent(event)鼠标释放事件:
void QGraphicsProxyWidget::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)   声明3
  • 1
  • 1
QPushButton控件是通过QWidget::mouseReleaseEvent事件来触发。mouseReleaseEvent事件传递类似于mousePressEvent事件
阅读全文
0 0
原创粉丝点击