-1

所以我试图在两点之间画一条线。鼠标左键单击开始线条,然后我希望在鼠标移动时动态绘制线条(几乎就像线条的预览)。再次单击鼠标左键,线条将被永久绘制。我知道还有很多其他关于 QPaintEvents 的帖子,并且我结合了一些使用的技术,但由于某种原因,没有任何东西被绘制到画布上。下面是代码:

void Main::mousePressEvent(QMouseEvent * event)
{
    if (event->button() == Qt::LeftButton) {
    QPointF pos = event->pos();
        if( mStartPoint.isNull() ) {
            if(josh.contains(pos))
                mStartPoint = pos;
        } else {
            canvas.addLine(mStartPoint.x(),mStartPoint.y(),pos.x(),pos.y());
            mStartPoint = QPointF();
        }
    }
}

bool Main::eventFilter(QObject *obj, QEvent *event)
{
  if (event->type() == QEvent::MouseMove)
  {
    QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
    if (!mStartPoint.isNull()) {
        m_targetImage = QImage(canvas.width(),canvas.height(),QImage::Format_ARGB32);
        QPainter p;
        p.begin(&m_targetImage);
        p.drawLine(mStartPoint, mouseEvent->pos());
        p.end();
    }
    statusBar()->showMessage(QString("Mouse move (%1,%2)").arg(mouseEvent->pos().x()).arg(mouseEvent->pos().y()));
  }
  return false;
}

void Main::paintEvent(QPaintEvent *pe)
{
        QPainter painter(this);
        QPen pen(Qt::red);
        pen.setWidth(10);
        painter.setPen(pen);
        painter.drawImage(0, 0, m_targetImage);
}

任何帮助表示赞赏!谢谢!乔什

4

2 回答 2

1

我想这就是你想要的。根据您的要求更改参数。

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

void Main::mousePressEvent(QMouseEvent* event)
{
      m_nbMousePressed = true;
      m_nInitialX = event->pos().x();
      m_nInitialY = event->pos().y();
}

void Main::mouseReleaseEvent(QMouseEvent *event)
{
m_nbMousePressed = false;
}

void Main::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 Main::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
}

这段代码会画出2 point line你想要的。

于 2012-07-27T10:00:35.153 回答
1

这是我的示例,说明如何直接在小部件上以您的方式绘制线条。

宣言:

private:
    void drawLines(QPainter *p);

    QPoint startPos;
    QPoint endPos;
    bool inDrawing;
    QVector<QLine> lines;

在构造函数中设置初始值:

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    startPos = QPoint();
    endPos = QPoint();
    inDrawing = false;
    setMouseTracking(true);
}

开始或结束画线:

void Widget::mousePressEvent(QMouseEvent *event)
{
    if (event->buttons() & Qt::LeftButton)
    {
        if (!inDrawing)
        {
            startPos = event->pos();
        }
        else
        {
            endPos = event->pos();

            QLine line = QLine(startPos, event->pos());
            lines.append(line);
        }

        inDrawing = !inDrawing;
    }
}

void Widget::mouseMoveEvent(QMouseEvent *event)
{
    if (inDrawing)
    {
        endPos = event->pos();
        update();
    }
}

绘制当前和保存的线:

void Widget::drawLines(QPainter *p)
{
    if (!startPos.isNull() && !endPos.isNull())
    {
        p->drawLine(startPos, endPos);
    }

    p->drawLines(lines);
}

绘画:

void Widget::paintEvent(QPaintEvent *event)
{
    QPainter p(this);
    QPen pen;
    pen.setColor(Qt::red);
    pen.setWidth(4);
    p.setPen(pen);

    drawLines(&p);
}
于 2012-07-27T10:06:05.260 回答