当设备像素比率设置为2x时,我正在尝试绘制高分辨率图像(以便在4K显示器上很好地显示)。 如果我直接在画家上画一个像素图,它可以正常工作:
int pixelRatio = 2;
QPixmap myImage = ...;
auto pxm = myImage.scaled(imgDiameter * pixelRatio, imgDiameter * pixelRatio, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
pxm.setDevicePixelRatio(pixelRatio);
painter->drawPixmap(QPoint(xPos, yPos), pxm);
然而,由于我需要在不规则形状上绘制图像,所以我使用画笔:
int pixelRatio = 2;
QPixmap myImage = ...;
auto oldBrush = painter->brush();
auto pxm = myImage.scaled(imgDiameter * pixelRatio, imgDiameter * pixelRatio, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
pxm.setDevicePixelRatio(pixelRatio);
QBrush brush(pxm);
painter->setPen(Qt::NoPen);
painter->setBrush(brush);
painter->setBrushOrigin(xPos, yPos);
painter->drawEllipse(xPos, yPos, imgDiameter, imgDiameter);
painter->setBrush(oldBrush);
当画笔被填满时,它似乎不符合pixmap的devicePixelRatio,因此形状内的像素图是两倍大。
我正在做的事情有问题吗?
答案 0 :(得分:0)
首先需要在pixmap上绘制你想要的,然后为pixmap设置所需的设备像素比率,然后绘制这个像素图
if (pix.devicePixelRatio() == 2.0) {
QPixmap output(pix.size());
output.fill(Qt::transparent);
QPainter pixPainter(&output);
pixPainter.setBrush(pix);
pixPainter.setPen(Qt::transparent);
pixPainter.setRenderHint(QPainter::Antialiasing);
QPainterPath path;
path.add(...);
...
pixPainter.drawPath(path);
output.setDevicePixelRatio(pix.devicePixelRatio());
painter.drawPixmap(0,0,output);
}