本文介绍了在Qt中用像素图笔画一条线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我正在使用Qt / C ++开发一个简单的绘图应用程序。

For some time I'm developing a simple drawing and painting app with Qt/C++.

当前我正在使用QPainter :: drawLine()进行绘制,并且工作正常。

Currently I'm using QPainter::drawLine() to draw, and it works fine.

什么我想要做的是使用pixmap笔刷进行绘制,这是我可以做到的方式。我可以使用QPainterPath和QPainter :: strokePath()用单色填充的像素图进行绘制。我用笔在像素图上用笔画了路径。

What I want to do is drawing with pixmap brushes, which in a way I can do. I can draw with single color filled pixmaps using QPainterPath and QPainter::strokePath(). I stroke the path with a pen using a brush with the pixmap.

如果您仍在阅读,我的问题是,如果我使用QPen和QPainter: strokePath()我得到了一条平铺的画笔行。但是我想将像素图沿线绘制。就像某些图像编辑器中基于图像的笔刷一样。我可以使用drawRect()来做到这一点,但这会把像素图分开。

In case you're still reading, my problem is, if I use a QPen and QPainter::strokePath() I get a line with tiled brush. But I want the pixmap to be drawn along the line. Like the image based brushes in some image editors. I can do that with drawRect(), but that draws the pixmaps apart.

如果您从我写的胡言乱语中了解我的问题,我怎么能用像素图画笔?

If you understand my problem from the gibberish I wrote, how can I draw a line with a pixmap brush?

编辑:
这是我目前正在做的事情:

Here's what I do currently:

void Canvas::mouseMoveEvent(QMouseEvent *event)
{
    polyLine[2] = polyLine[1];
    polyLine[1] = polyLine[0];
    polyLine[0] = event->pos();

    //Some stuff here
    painter.drawLine(polyLine[1], event->pos());
}

这是我尝试过的:

void Canvas::mouseMoveEvent(QMouseEvent *event)
{
    QPen pen(brush, brushSize, Qt::SolidLine, Qt::RoundCap, Qt::BevelJoin);
    //Some stuff here
    path.lineTo(event->pos());
    painter.strokePath(path, pen);
    //This creates a fine line, but with a tiled brush
}

要沿着鼠标移动绘制像素图,我尝试

To draw a pixmap along mouse movement, I tried

void Canvas::mouseMoveEvent(QMouseEvent *event)
{
    //Some stuff
    QBrush brush(QPixmap(":images/fileName.png"));
    painter.setBrush(brush);
    painter.setPen(Qt::NoPen);
    painter.drawRect(QRect(event->pos() - brushSize / 2, event->pos() - brushSize / 2, brushSize, brushSize));
    //This draws the pixmaps with intervals.
}


推荐答案

没关系,我发现了解决方案

Nevermind, I've found the solution here

已接受答案显示了如何沿着路径重复绘制像素图。那很棒。作为参考,我将代码复制到这里:

The accepted answer shows how to draw a pixmap repeatedly along a path. That's great. For reference, I'll copy the code here:

QPointF lastPosition, currentPosition;
qreal spacing;

void draw() {
    QPainterPath path;
    path.moveTo(lastPosition);
    path.lineTo(currentPosition);
    qreal length = path.length();
    qreal pos = 0;

    while (pos < length) {
        qreal percent = path.percentAtLength(pos);
        drawYourPixmapAt(path.pointAtPercent(percent)); // pseudo method, use QPainter and your brush pixmap instead
        pos += spacing;
    }
}

这篇关于在Qt中用像素图笔画一条线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 02:39