首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用图像填充Qt-圈

用图像填充Qt-圈
EN

Stack Overflow用户
提问于 2015-06-22 12:52:37
回答 2查看 2.5K关注 0票数 3

我试着用四张图片填充圆圈。首先,每个照片刷在相同的大小,今后比例尺最终图像与该大小。但结果不是我想要的。

此刻,前景中的圆圈和背景上的照片,如下所示:

如何填充圆圈的照片和删除矩形?

这是我的代码:

代码语言:javascript
复制
QPixmap *CGlobalZone::profPicFromFourPics(QList<QPixmap> pixmapList)
{
        QPixmap *avatar = NULL;
        QImage roundedImage(CGlobalZone::AVATAR_WIDTH_M*2, CGlobalZone::AVATAR_HEIGHT_M*2, QImage::Format_ARGB32);
        roundedImage.fill(Qt::transparent);
        QBrush brush0(pixmapList[0]);
        QBrush brush1(pixmapList[1]);
        QBrush brush2(pixmapList[2]);
        QBrush brush3(pixmapList[3]);
        QPainter painter(&roundedImage);
        QPen pen(QColor(176, 216, 242), 1);
        painter.setRenderHint(QPainter::Antialiasing);
        painter.setBrush(brush0);
        painter.drawRect(0 , 0 , CGlobalZone::AVATAR_WIDTH_M  , CGlobalZone::AVATAR_HEIGHT_M  );
        painter.setBrush(brush1);
        painter.drawRect(CGlobalZone::AVATAR_WIDTH_M , 0 , CGlobalZone::AVATAR_WIDTH_M*2  , CGlobalZone::AVATAR_HEIGHT_M  );
        painter.setBrush(brush2);
        painter.drawRect(CGlobalZone::AVATAR_WIDTH_M , CGlobalZone::AVATAR_HEIGHT_M , CGlobalZone::AVATAR_WIDTH_M*2  , CGlobalZone::AVATAR_HEIGHT_M*2  );
        painter.setBrush(brush3);
        painter.drawRect(0 , CGlobalZone::AVATAR_HEIGHT_M , CGlobalZone::AVATAR_WIDTH_M*2  , CGlobalZone::AVATAR_HEIGHT_M*2  );
        painter.drawEllipse(0, 0, CGlobalZone::AVATAR_WIDTH_M*2-3 , CGlobalZone::AVATAR_HEIGHT_M*2-3 );
        avatar  = new QPixmap(QPixmap::fromImage(roundedImage).scaled(QSize(CGlobalZone::AVATAR_WIDTH_M, CGlobalZone::AVATAR_HEIGHT_M),
                                                    Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation));
       return avatar;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-06-22 13:52:19

我这样做的方式如下(详见源评论):

代码语言:javascript
复制
// The avatar image. Should be four, but use one for demonstration.
QPixmap source("avatar.png");

// Initialize the avatar and bring it to a standard size.
// This step may be skipped if avatars have the same sizes.
const int width = CGlobalZone::AVATAR_WIDTH_M;
const int height = CGlobalZone::AVATAR_HEIGHT_M;
source = source.scaled(width, height);

// Set up the final image that contains four avatar images.
QPixmap target(2 * width, 2 * height);
target.fill(Qt::transparent);

QPainter painter(&target);

// Set clipped region (circle) in the center of the target image
QRegion r(QRect(width / 2, height / 2, width, height), QRegion::Ellipse);
painter.setClipRegion(r);

painter.drawPixmap(0, 0, source);          // First avatar
painter.drawPixmap(width, 0, source);      // Second avatar
painter.drawPixmap(0, height, source);     // Third avatar
painter.drawPixmap(width, height, source); // Fourth avatar

target.save("test.png");
票数 2
EN

Stack Overflow用户

发布于 2015-06-22 13:36:01

对任务使用画家路径。而不是drawEllipse,你应该做

代码语言:javascript
复制
int dim = CGlobalZone::AVATAR_WIDTH_M*2;
QPainterPath entirePath;
QPainterPath ellipsePath;

entirePath.addRect(0, 0, dim, dim);
ellipsePath.addEllipse(0, 0, dim-3, dim-3);
QPainterPath outOfEllipse = entirePath.subtracted(ellipsePath);
painter.fillPath(outOfEllipse, QBrush(Qt::transparent));

编辑:,因为QPainterPath用于复杂的情况,所以您应该使用QRegion。经过测试,我发现在填充相同路径的内部和外部时,可能会出现小的像素错误(在您的例子中,会很好)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30980484

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档