Search code examples
qtmouseeventqgraphicssceneqgraphicsitemmousehover

Using both MouseMoveEvent for QGraphicsScene and HoverEnterEvent for QGraphicsItem


I'm trying to create a program in which you can connect points together with lines. I instantiate QGraphicsEllipseItem into a QGraphicsScene and I use HoverEnterEvent and HoverLeaveEvent to change the color and the size of the ellipses when the mouse is over them. To draw a temporary line between the point clicked and the mouse cursor I have to use MouseMoveEvent into the scene. However, when I do that, the HoverEvents of the items don't work anymore ! How can I use both MouseMoveEvent of the scene and HoverEvents of the items ?

enter image description here

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();
    }
}


Solution

  • By default QGraphicsScene::mouseMoveEvent sends the necessary information to handle the hover event of the items but override that method you eliminate that behavior. The solution is to call the parent method.

    #include <QtWidgets>
    
    class GraphicsScene: public QGraphicsScene{
    public:
        using QGraphicsScene::QGraphicsScene;
    protected:
        void mousePressEvent(QGraphicsSceneMouseEvent *event){
            if(!m_lineitem){
                m_lineitem = new QGraphicsLineItem;
                addItem(m_lineitem);
            }
            QLineF l(event->scenePos(), event->scenePos());
            m_lineitem->setLine(l);
            QGraphicsScene::mousePressEvent(event);
        }
        void mouseMoveEvent(QGraphicsSceneMouseEvent *event){
            if(m_lineitem){
                QLineF l(m_lineitem->line().p1(), event->scenePos());
                m_lineitem->setLine(l);
            }
            QGraphicsScene::mouseMoveEvent(event);
        }
    private:
        QGraphicsLineItem *m_lineitem = nullptr;
    };
    
    class Point: public QGraphicsEllipseItem{
    public:
        Point(QGraphicsItem *parent=nullptr): QGraphicsEllipseItem(parent){
            setRect(QRectF(-5, -5, 10, 10));
            QPen pen;
            pen.setColor(Qt::lightGray);
            pen.setWidth(1);
            setPen(pen);
            setAcceptHoverEvents(true);
        }
    protected:
        void hoverEnterEvent(QGraphicsSceneHoverEvent *event){
            QPen pen;
            pen.setColor(Qt::green);
            pen.setWidth(2);
            setPen(pen);
            QGraphicsEllipseItem::hoverEnterEvent(event);
        }
        void hoverLeaveEvent(QGraphicsSceneHoverEvent *event){
            QPen pen;
            pen.setColor(Qt::lightGray);
            pen.setWidth(1);
            setPen(pen);
            QGraphicsEllipseItem::hoverLeaveEvent(event);
        }
    };
    
    int main(int argc, char *argv[]){
        QApplication a(argc, argv);
        GraphicsScene *scene = new GraphicsScene;
        QGraphicsView w(scene);
        w.setRenderHint(QPainter::Antialiasing, true);
        w.fitInView(QRectF(0, 0, 100, 100), Qt::KeepAspectRatio);
        for(const QPointF & p: {QPointF(10.0, 10.0), QPointF(90.0, 20.0), QPointF(30.0, 40.0)}){
            Point *it = new Point();
            scene->addItem(it);
            it->setPos(p);
        }
        w.resize(640, 480);
        w.show();
        return a.exec();
    }