当前位置:网站首页>1.QImage 填充透明画笔;2.path.addText如何添加换行
1.QImage 填充透明画笔;2.path.addText如何添加换行
2022-07-22 09:12:00 【氺月洞天】
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;
}
边栏推荐
- 周末和技术大咖们聚餐,聊到了软件测试行业的“金九银十”高峰【内卷之势已然形成】
- [STM32] STM32 SDIO SD card read / write test (II) -- SD_ Power on phase of init
- 【SDIO】SDIO、SD卡、FatFs文件系统相关文章索引
- 使用工厂的方法创建对象
- juju debug hacks (by quqi99)
- LeetCode: 197. 上升的温度
- node生成token与验证token(typeScript语法)
- Interview experience of Android Internet manufacturers
- 总结20220118(二叉树)
- [STM32] STM32 SDIO SD card read / write test (IV) -- SD_ Transfer mode phase of test
猜你喜欢
随机推荐
01. Open closed principle
IP, subnet mask, gateway, IPS and IDS
Unity:素材下载
ECSHOP prompts "the template file themesmobile/mo_paleng_mobile/index.dwt cannot be modified"?
SQL injection
位与:一个数&1的结果
04. interface aggregation principle
node生成token与验证token(typeScript语法)
Web novice zone
【Nordic】nRF52810 OTA升级(二)–DFU如何使用
Js高级-词法作用域
[FatFs] FAT32 file system protocol summary (Theory + Practice)
总结20220209
C语言简易TCP服务端程序
ip,子网掩码,网关,IPS与IDS
协议与端口
【FatFs】基于STM32 SD卡移植FatFs文件系统
Wechat official account web page authorization ----- redirect_ The URI domain name is inconsistent with the background configuration, and the error code is 10003
Method of getting node binding data data index by applet
centos7.5下添加gd库然后mysql拓展库没了mysql拓展的配置也没问题,phpinfo中就是没有mysql拓展