gpt4 book ai didi

c++ - qt 使用 QPainterPath 绘制轮廓文本

转载 作者:行者123 更新时间:2023-11-30 04:53:27 25 4
gpt4 key购买 nike

我必须像这样在 QImage 上绘制带轮廓线的删除文本:

enter image description here

我是这样做的:

QPainter painter(this);

QPainterPath path;

QFont font;
font.setPixelSize(95);
font.setStrikeOut(true);
font.setBold(true);

path.addText(10, 150, font, "lololo");

painter.setPen(Qt::blue);
painter.setBrush(Qt::red);

painter.drawPath(path);

得到这个结果:

enter image description here

正如您所看到的那样,划线具有斑马状填充。我怎样才能用画家的画笔完全填满它?

我尝试更改 QPainter 合成模式但没有成功。我也尝试使用 QPainterPathStroker 得到相同的结果。

当然我可以用普通字体(未删除)加上矩形绘制删除文本,但这不是一个漂亮的解决方案。

最佳答案

解决方案是在有和没有罢工的两条路径之间执行操作:

#include <QtWidgets>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QImage image(300, 200, QImage::Format_ARGB32);
image.fill(Qt::transparent);

QPoint p(30, 150);
QString text = "lololo";

QFont font;
font.setPixelSize(95);
font.setBold(true);

QPainterPath path1;
font.setStrikeOut(true);
path1.addText(p, font, text);
font.setStrikeOut(false);
QPainterPath path2;
path2.addText(p, font, text);

QPainterPath strike = (path1 + path2) - (path1 & path2);
// \---join---/ \-intersection-/
QPainter painter(&image);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::blue);
painter.setBrush(Qt::red);
painter.drawPath(path2);
painter.drawPath(strike);
painter.end();

QLabel w;
w.setPixmap(QPixmap::fromImage(image));
w.show();

return a.exec();
}

enter image description here

关于c++ - qt 使用 QPainterPath 绘制轮廓文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53911856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com