1.QImage 填充透明画笔;2.path.addText如何添加换行

1.QImage 填充透明画笔

问题如下:

QImage如何填充一个透明的path

解决1

rect = path.boundingrect();

遍历 rect => (x, y), if(path.contains(x, y)) img.setPixel(x, y, transparent);

这样绘制的画笔会有严重的粗细反差,效果很差

解决2

优化解决1,将path转成对于的list<point>,然后再次setFixel即可

QList<QPoint> Form::path2Points(const QPainterPath &path)
{
    QList<QPoint> curPoints;
    QRect r = path.boundingRect().toRect();
    QImage img(r.x() + r.width(), r.y() + r.height(), QImage::Format_ARGB32);
    img.fill(qRgba(255, 255, 255, 255));
    QPainter p(&img);
    p.fillPath(path, QColor(0, 0, 0, 255));
    for(int x = r.x(); x < r.x() + r.width(); x ++) {
        for(int y = r.y(); y < r.y() + r.height(); y ++) {
            if(img.pixel(x, y) == qRgba(0, 0, 0, 255)
                    && img.rect().contains(x, y)) {
                curPoints << QPoint(x, y);
            }
        }
    }
    return curPoints;
}

 

2.path.addText如何添加换行

文档里

void QPainter::drawText(const QPointF &position, const QString &text)

Draws the given text with the currently defined text direction, beginning at the given position.

This function does not handle the newline character (\n), as it cannot break text into multiple lines, and it cannot display the newline character. Use the QPainter::drawText() overload that takes a rectangle instead if you want to draw multiple lines of text with the newline character, or if you want the text to be wrapped.

By default, QPainter draws text anti-aliased.

Note: The y-position is used as the baseline of the font.

但是path.addText里没有rect的参数,这就很麻烦了

自定义pathText 的函数如下

QPainterPath Form::pathText(int x, int y, QFont font, const QString &text)
{
    QPainterPath path;

    qreal lineHeight = QFontMetricsF(font).height();
    int lineCount = 1;
    QByteArray content = text.toUtf8();
    QBuffer buff(&content);
    buff.open(QBuffer::ReadOnly);
    while(!buff.atEnd()) {
        QByteArray line = buff.readLine();
        path.addText(x, y + lineCount * lineHeight, font, line);
        lineCount++;
    }
    return path;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值