【发布时间】:2020-02-09 13:58:08
【问题描述】:
我正在尝试创建一个程序,您可以在其中将点与线连接在一起。我将 QGraphicsEllipseItem 实例化为 QGraphicsScene,当鼠标悬停在椭圆上时,我使用 HoverEnterEvent 和 HoverLeaveEvent 来更改椭圆的颜色和大小。要在单击的点和鼠标光标之间画一条临时线,我必须在场景中使用 MouseMoveEvent。但是,当我这样做时,项目的 HoverEvents 不再起作用了!如何同时使用场景的 MouseMoveEvent 和项目的 HoverEvents ?
void GraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
for (int i = 0; i < pointList.size(); ++i) {
if (pointList[i]->over == 1){
pointList[i]->press();
lineActivated=true;
tempLine.setLine(pointList[i]->x(),pointList[i]->y(),pointList[i]->x(),pointList[i]->y());
}
}
}
void GraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if(lineActivated){
const QPointF pos = event->scenePos();
tempLine.setP2(pos);
}
}
void Point::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
pen.setColor(Qt::green);
pen.setWidth(2);
this->setPen(pen);
over=true;
qDebug("enter");
update();
}
void Point::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
if(isClicked==false){
pen.setColor(Qt::lightGray);
pen.setWidth(1);
over=false;
this->setPen(pen);
qDebug("leave");
update();
}
}
【问题讨论】:
标签: qt mouseevent qgraphicsscene qgraphicsitem mousehover