【问题标题】:Using both MouseMoveEvent for QGraphicsScene and HoverEnterEvent for QGraphicsItem使用 QGraphicsScene 的 MouseMoveEvent 和 QGraphicsItem 的 HoverEnterEvent
【发布时间】: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


    【解决方案1】:

    默认情况下,QGraphicsScene::mouseMoveEvent 发送必要的信息来处理项目的悬停事件,但覆盖该方法可以消除该行为。解决方法是调用父方法。

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

    【讨论】:

    • 你给我的信息对我帮助很大!但是,mouseMoveEvent 仅在我按下鼠标按钮时才起作用。我想要的是,在我点击后线条跟随鼠标,当我第二次点击时,线条被绘制出来。我看到了 QGraphicsView 的 setMouseTracking,但没有看到 QGraphicsScene。
    • 好的,它确实有效!我在窗口而不是视图上设置了鼠标跟踪。再次感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多