在Qt图形视图中跟踪折线

Tracking polyline in graphics view in Qt

本文关键字:跟踪 折线 视图 Qt 图形      更新时间:2023-10-16

如何在图形视图中使用QPainter类跟踪鼠标折线和样条?我粘贴了我的代码。请检查和编辑。我正在生成一个折线上点击一个按钮。polyline.h

#include <QGraphicsSceneMouseEvent>
#include <QMouseEvent>
#include <QDebug>
#include "ui_mainwindow.h"
#include "qmath.h"
class polyline: public QObject, public QGraphicsItem
{
     Q_OBJECT
public:
    polyline();
    QRectF boundingRect() const;
    virtual void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget);
protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *e);
    void mouseMoveEvent(QGraphicsSceneMouseEvent *e);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *e);
private:
    int x1, y1, x2, y2, w, h;
    bool mFirstClick;
    bool mSecondClick;
    bool mPaintFlag;
    QVector<QPointF> stuff;
};
#endif // POLYLINE_H

polyline.cpp

 #include "polyline.h"
#include "QDebug"
polyline::polyline()
{
    mFirstClick = true;
    mSecondClick = false;
    mPaintFlag = false;
}
QRectF polyline::boundingRect() const
{
    // outer most edges
    return QRectF(0,0,800,800);
}
    void polyline::mousePressEvent(QGraphicsSceneMouseEvent* e){
        if(e->button()==Qt::LeftButton) {
            if(mFirstClick){
                x1 = e->pos().x();
                y1 = e->pos().y();
                mFirstClick = false;
                mSecondClick = true;
            }
            else if(!mFirstClick && mSecondClick){
                x2 = e->pos().x();
                y2 = e->pos().y();
                mPaintFlag = true;
                mSecondClick = false;
                update();
            }
        }
        QGraphicsItem::mousePressEvent(e);
        update();
    }
    void polyline:: paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
        QRectF rect = boundingRect();
        if(mPaintFlag){
            QPen paintpen(Qt::red);
            paintpen.setWidth(4);
            QPen linepen(Qt::black);
            linepen.setWidth(1);
            QPoint p1;
            p1.setX(x1);
            p1.setY(y1);
            painter->setPen(paintpen);
            painter->drawPoint(p1);
            QPoint p2;
            p2.setX(x2);
            p2.setY(y2);
            painter->setPen(paintpen);
            painter->drawPoint(p2);
            painter->setPen(linepen);
            painter->drawLine(p1, p2);
        }
    }
    void polyline::mouseMoveEvent(QGraphicsSceneMouseEvent *e)
    {
            m_pCurrentLine->setx2(e->x());
            m_pCurrentLine->sety2(e->y());
            m_pCurrentLine->update();
        }
        QGraphicsItem::mouseMoveEvent(e);
    }
    void  polyline::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
    {
        QGraphicsItem::mouseReleaseEvent(e);
        update();
    }

创建从QGraphicsView派生的自己的类并重新实现mouseMoveEvent:

void OwnGraphicsView::mouseMoveEvent(QMouseEvent *event)
{
    m_pCurrentLine->setX2(event->x());
    m_pCurrentLine->setY2(event->y());
    m_pCurrentLine->update();
}

使用GraphicsView为小部件设置鼠标跟踪:

setMouseTracking(true);

创建一个从QGraphicsItem派生的Item,例如一个类"line",并重新实现paint Method:

void line:: paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)

因此每次调用mouseMoveEvent时,更新您的线条坐标并重新绘制它。

希望对大家有所帮助。