在 QWidget 上动态绘制一条线

Posted

技术标签:

【中文标题】在 QWidget 上动态绘制一条线【英文标题】:Drawing a line dynamically on QtWidget 【发布时间】:2015-04-01 00:28:06 【问题描述】:

这是在小部件上画线的代码。问题是它保留了先前绘制的线,并且在每次鼠标移动时它都会一次又一次地绘制。我想像 mspaint 一样绘制,即只有在鼠标释放事件时它才会完成线条绘制(否则只是线条的预览)。我想到的一个想法是在每次鼠标移动时删除预览线,另一个是在一些临时视图上绘制并在鼠标释放时完成它。但是很难找到正确的方法来做到这一点。 感谢您的帮助

#include "paintwidget.h"
#include "ui_paintwidget.h"

#include <QtGui>

paintWidget::paintWidget(QWidget* parent)
    : QWidget(parent)
    , ui(new Ui::paintWidget)

    ui->setupUi(this);

    m_nInitialX = 0;
    m_nInitialY = 0;
    m_nFinalX = 0;
    m_nFinalY = 0;
    m_nPTargetPixmap = 0;
    m_nPTargetPixmap = new QPixmap(400, 400);
    m_nbMousePressed = false;


paintWidget::~paintWidget()

    delete ui;


void paintWidget::mousePressEvent(QMouseEvent* event)

    m_nbMousePressed = true;
    m_nInitialX = event->pos().x();
    m_nInitialY = event->pos().y();


void paintWidget::mouseReleaseEvent(QMouseEvent* event)

    m_nbMousePressed = false;
    //update();


void paintWidget::paintEvent(QPaintEvent* e)

    if (m_nbMousePressed) 
        QPainter PixmapPainter(m_nPTargetPixmap);
        QPen pen(Qt::green);
        PixmapPainter.setPen(pen);
        //PixmapPainter.drawLine(m_nInitialX, m_nInitialY, m_nFinalX, m_nFinalY);
    
    QPainter painter(this);
    painter.drawPixmap(0, 0, *m_nPTargetPixmap);


void paintWidget::mouseMoveEvent(QMouseEvent* event)

    if (event->type() == QEvent::MouseMove) 
        QPainter PixmapPainter(m_nPTargetPixmap);
        QPen pen(Qt::black);
        PixmapPainter.setPen(pen);
        PixmapPainter.drawLine(m_nInitialX, m_nInitialY, m_nFinalX, m_nFinalY);
        update(); // update your view
        m_nFinalX = event->pos().x();
        m_nFinalY = event->pos().y();
    
    update(); // update your view

【问题讨论】:

【参考方案1】:

我把来源放在这里:

https://github.com/peteristhegreat/persistent_paint

注意添加的 cmets

这里是相关文件:

#include "paintwidget.h"
#include <QPainter>
#include <QPen>
#include <QBrush>
#include <QDebug>

PaintWidget::PaintWidget(QWidget *parent) : QWidget(parent)

// the ui form wasn't needed here, so I added it without it.

// QLine neatly keeps track of both the two points, m_line in this case
//    m_nInitialX = 0;
//    m_nInitialY = 0;
//    m_nFinalX = 0;
//    m_nFinalY = 0;
//    m_nPTargetPixmap = 0;
//    m_nPTargetPixmap = new QPixmap(400,400);
    m_nPTargetPixmap = QPixmap(400,400);// put the pixmap on the stack instead of the heap
    m_nPTargetPixmap.fill();
    m_nbMousePressed = false;


PaintWidget::~PaintWidget()

//    delete ui;


void PaintWidget::mousePressEvent(QMouseEvent* event)

    m_nbMousePressed = true;
//    m_nInitialX = event->pos().x();
//    m_nInitialY = event->pos().y();
    m_line.setP1(event->pos());
    m_line.setP2(event->pos());


void PaintWidget::mouseReleaseEvent(QMouseEvent *event)

    m_nbMousePressed = false;
    update();


void PaintWidget::paintEvent(QPaintEvent *e)

    static bool wasPressed = false;
    QPainter painter(this);

    if(m_nbMousePressed)
    
        painter.drawPixmap(0, 0, m_nPTargetPixmap);
        painter.drawLine(m_line);
        wasPressed = true;
    
    else if(wasPressed)
    
        // Note that this painting only needs to happen once,
        // right when the mouse is released.
        QPainter PixmapPainter(&m_nPTargetPixmap);
        QPen pen(Qt::green);
        PixmapPainter.setPen(pen);
        PixmapPainter.drawLine(m_line);

        painter.drawPixmap(0, 0, m_nPTargetPixmap);
        wasPressed = false;
    


void PaintWidget::mouseMoveEvent(QMouseEvent *event)

    if (event->type() == QEvent::MouseMove)
    
//        QPainter PixmapPainter(m_nPTargetPixmap);
//        QPen pen(Qt::black);
//        PixmapPainter.setPen(pen);
//        PixmapPainter.drawLine(m_nInitialX, m_nInitialY, m_nFinalX, m_nFinalY);
//        m_nFinalX = event->pos().x();
//        m_nFinalY = event->pos().y();
        m_line.setP2(event->pos());
//        update(); // update your view
    
    update(); // update your view


更新:使用 QGraphicsScene 和绘制线条和椭圆的附加信息:

QGraphicsScene 具有可以在单击场景时添加的线条和椭圆。

How to draw a point (on mouseclick) on a QGraphicsScene?

Arc in QGraphicsScene

http://doc.qt.io/qt-5/graphicsview.html

http://doc.qt.io/qt-5/examples-graphicsview.html

这个例子似乎特别相似:

http://doc.qt.io/qt-5/qtwidgets-graphicsview-diagramscene-example.html

希望对您有所帮助。

【讨论】:

我的理解是 m_nPTargetPixmap 作为一个额外的绘图层工作,当鼠标被释放时,它会在画家身上绘画。对吗? 是的。最先绘制的在底部,最后绘制的在顶部。

以上是关于在 QWidget 上动态绘制一条线的主要内容,如果未能解决你的问题,请参考以下文章

如何在没有中间绘制的情况下调整 QWidget 的大小

如何使用opencv在图像上动态画一条线? [提供的代码,无法找出错误]

程序不使用 QPainter 画线

如何在 QWidget 中绘制简单的水平线?

由父级绘制 QWidget

在 QWidget 上绘画以覆盖整个小部件