直接重写鼠标事件,遇到的问题是:只响应press事件,而move和release事件不响应。
解决方式是将item设为selectable或movable。
//构造函数中
this->setFlag(QGraphicsItem::ItemIsSelectable);
this->setAcceptedMouseButtons(Qt::LeftButton);
this->setAcceptHoverEvents(true);//重写hover事件需添加
void Rectangle::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
qDebug()<<"EVENT: "<<"MOUSE PRESS"<<endl;
}
BasicItemAbs::mousePressEvent(event);
}
void Rectangle::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
qDebug()<<"EVENT: "<<"MOUSE MOVE"<<endl;
BasicItemAbs::mouseMoveEvent(event);
}
void Rectangle::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
qDebug()<<"EVENT: "<<"MOUSE RELEASE"<<endl;
}
BasicItemAbs::mouseReleaseEvent(event);
}
评论区