簡體   English   中英

Qt-用圖像填充圓圈

[英]Qt- fill circle with images

我試圖用四個圖像填充圓圈。 首先,每個相同尺寸的照片刷,此后縮放具有該尺寸的最終圖像。 但結果不是我想要的。

此刻前景中的圓圈和背景上的照片,如下所示:
在此輸入圖像描述
如何用照片填充圓圈並刪除矩形?

這是我的代碼:

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

使用畫家路徑完成任務。 而不是drawEllipse你應該做

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 經過測試,我發現在同一路徑內部和外部填充時可能會出現小像素錯誤(在您的情況下,它會很好)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM