c++ - Qt-用图像填充圆圈

标签 c++ qt qpainter

我尝试用四张图片填充圆圈。首先,每个照片画笔大小相同,之后用该大小缩放最终图像。但结果不是我想要的。

目前前景是圆圈,背景是照片,像这样:
enter image description here
如何用照片填充圆并去除矩形?

这是我的代码:

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;
}

最佳答案

我将通过以下方式执行此操作(源评论中的详细信息):

// 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");

关于c++ - Qt-用图像填充圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30980484/

相关文章:

c++ - 将扫描码发送到我的应用程序

qt - qt 快速 QML 应用程序的自定义样式页面(如 HTML 和 CSS)

c++ - qt类什么时候作为智能指针使用

c++ - Qt:制作不同颜色的堆叠条形图

c++ - 在使用 QPainterPath 绘制自由路径时找到交叉线

c++ - 在 C++ 中使用 directshow 过滤器从视频中捕获帧

c++ - 为什么 std::partition 没有异位变体?

c++ - 我对 C++ 中的复制构造函数感到困惑

c++ - 稍后初始化的小部件将不会显示在主窗口中

c++ - 使用QPainter连接不同粗细的线条形成干净的边框