您的位置:首页 > Web前端 > CSS

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

2015-12-10 10:12 701 查看
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事件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: