这是在窗口小部件上绘制线条的代码。问题是它保持先前绘制的线条并且在每个鼠标上移动它一次又一次地绘制。我想像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
}
答案 0 :(得分:2)
我把我的来源放在这里:
https://github.com/peteristhegreat/persistent_paint
注意添加的评论
以下是相关文件:
#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?
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
希望有所帮助。