2012-01-01 4 views
0

사용자 정의 된 QWidget paintEvent 메소드에서 원 모양의 이미지 아이콘이있는 원을 그립니다. 원본 이미지가 파일에서로드 된 다음 QPainter 구성을 사용하여 자동으로 원으로 캐스팅됩니다. 그것을하는 방법? 고맙습니다!이미지 파일에서 원 아이콘을 만드는 방법은 무엇입니까?

void DotGraphView::paintNodes(QPainter & painter) 
{ 
    painter.setPen(Qt::blue); 
    painter.drawEllipse(x, y, 36, 36); 
    QPixmap icon("./image.png"); 
    QImage fixedImage(64, 64, QImage::Format_ARGB32_Premultiplied); 
    QPainter imgPainter(&fixedImage); 
    imgPainter.setCompositionMode(QPainter::CompositionMode_SourceIn); 
    imgPainter.drawPixmap(0, 0, 64, 64, icon); 
    imgPainter.setCompositionMode(QPainter::CompositionMode_SourceIn); 
    imgPainter.setBrush(Qt::transparent); 
    imgPainter.drawEllipse(32, 32, 30, 30); 
    imgPainter.end(); 
    painter.drawPixmap(x, y, 64, 64, QPixmap::fromImage(fixedImage)); 
} 

위의 코드는 작동하지 않습니다. 출력 디스플레이는 원 모양의 이미지가 아닙니다.

+1

어떻게 작동하지 않는지 자세히 설명해주십시오. 그것은 컴파일합니까? 그것은 전혀 실행됩니까? 그것은 잘못된 출력을 생산합니까? 어떤 식으로? –

+0

출력 디스플레이가 원 모양의 이미지가 아닙니다. – allenchen

+0

정확히 무엇입니까? 스크린 샷을 업로드 할 수 있습니까? – Cydonia7

답변

0

당신은 비교적 간단 클리핑 패스와 함께이 작업을 수행 할 수 있습니다

QPainter painter(this); 
painter.setPen(Qt::blue); 
painter.drawEllipse(30, 30, 36, 36); 
QPixmap icon("./image.png"); 

QImage fixedImage(64, 64, QImage::Format_ARGB32_Premultiplied); 
fixedImage.fill(0); // Make sure you don't have garbage in there 

QPainter imgPainter(&fixedImage); 
QPainterPath clip; 
clip.addEllipse(32, 32, 30, 30); // this is the shape we want to clip to 
imgPainter.setClipPath(clip); 
imgPainter.drawPixmap(0, 0, 64, 64, icon); 
imgPainter.end(); 

painter.drawPixmap(0, 0, 64, 64, QPixmap::fromImage(fixedImage)); 

(. 자주 이렇게하면 나는 픽스맵 캐시 것)

3
내가 제대로 이해하면 나도 몰라

, 그러나 이것은 당신이 원하는 것을 할 수 있습니다

#include <QtGui/QApplication> 
#include <QLabel> 
#include <QPixmap> 
#include <QBitmap> 
#include <QPainter> 

int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 

    // Load the source image. 
    QPixmap original(QString("/path/here.jpg")); 
    if (original.isNull()) { 
     qFatal("Failed to load."); 
     return -1; 
    } 

    // Draw the mask. 
    QBitmap mask(original.size()); 
    QPainter painter(&mask); 
    mask.fill(Qt::white); 
    painter.setBrush(Qt::black); 
    painter.drawEllipse(QPoint(mask.width()/2, mask.height()/2), 100, 100); 

    // Draw the final image. 
    original.setMask(mask); 

    // Show the result on the screen. 
    QLabel label; 
    label.setPixmap(original); 
    label.show(); 

    return a.exec(); 
} 

캐시에게 그 결과를 귀하는 QWidget 서브 클래스와 블리트에서 화면에 필요한 경계 RECT 당신의 페인트 이벤트의 요청이있을 때.

관련 문제