遇到的问题:
图B是直接drawPixmap原图
图A是qbrush调用setTextturre(原图)
然后painter.setbrush(该brush)
painter.drawRect(同样大小的矩形)
=======================================
为什么图A就不能从原图的左上角开始画?
解决方法:
QBrush brush;
brush.setTexture(drawQRPix(color, qrSize));
painter->setBrush(brush);
painter->setBrushOrigin(-qrSize.width()/2.0, -qrSize.height()/2.0);//关键是这句
另附QPixmap逐像素颜色变更为Size大小图像函数(我这边是对一张底色为白色的QRCode做颜色变更):
QPixmap CNode::drawQRPix(QColor color, QSize size)
{
QPixmap sourcePixmap(":/Icons/QR");
QImage image = sourcePixmap.scaled(size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation).toImage();
for(int w = 0; w < image.width(); ++w)
{
for(int h = 0; h < image.height(); ++h)
{
QRgb rgb = image.pixel(w, h);
if(rgb != QColor(Qt::white).rgb())//白色底色不改变
{
image.setPixel(w, h, color.rgba());
}
}
}
return QPixmap::fromImage(image);
}
网友评论