void QGraphicsItem::installSceneEventFilter(QGraphicsItem *filterItem)
class LabCrossEvent : public QGraphicsObject
{
Q_OBJECT
public:
LabCrossEvent() {}
virtual ~LabCrossEvent() {}
virtual QRectF boundingRect() const
{
return QRectF();
}
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = Q_NULLPTR)
{
}
signals:
void hover();
public slots:
protected:
bool LabCrossEvent::sceneEventFilter(QGraphicsItem * watched, QEvent * event)
{
qDebug() << "||" << event->type();
if(event->type() == QEvent::GraphicsSceneMove)
{
emit hover(); // signal that the item was moved
}
//return sceneEventFilter(watched,event);
return false; // pass the event to the original target item
}
};
QGraphicsItem* dL_Item = this->addRect(-22,-std::abs(high_dL),44,height_dL,QPen(QColor("#239B56")));
//安装的过场景事件滤器必须添加到场景中,否则事件过滤器无效
this->addItem(&_labCrossEvent);
//为矩形图元安装一个场景事件过滤器
dL_Item->installSceneEventFilter(&_labCrossEvent);