Qt 캔버스 페인터 - 헬로 위젯 예제

QCanvasPainterQCanvasPainterWidget 의 사용을 보여줍니다.

이 예제는 QCanvasPainterWidget 서브 클래스를 구현합니다. 그런 다음 이 위젯의 하나 이상의 인스턴스를 QMainWindow 내부의 QMdiArea 에 추가할 수 있습니다.

QCanvasPainterWidget 자체는 QRhiWidget 에서 파생되며 항상 QRhi 을 통해 가속화된 3D 렌더링을 사용합니다.

QCanvasPainterWidget 의 서브클래스는 최소한 paint()을 구현해야 합니다. 이 예제에서도 PNG 파일에서 로드한 이미지를 사용합니다.

class CanvasWidget : public QCanvasPainterWidget
{
public:
    CanvasWidget();
    void initializeResources(QCanvasPainter *p) override;
    void paint(QCanvasPainter *p) override;
    void graphicsResourcesInvalidated() override;

private:
    QCanvasImage m_image;
};

paint() 함수는 QCanvasPainter 공급자를 사용하여 바로 그리기를 시작할 수 있습니다:

void CanvasWidget::paint(QCanvasPainter *p)
{
    const float size = std::min(width(), height());
    const float centerX = width() / 2;
    const float centerY = height() / 2;

    // Paint the background circle
    QCanvasRadialGradient gradient1(centerX, centerY - size * 0.1f, size * 0.6f);
    gradient1.setStartColor(QColor(0x909090));
    gradient1.setEndColor(QColor(0x404040));
    p->beginPath();
    p->circle(QPointF(centerX, centerY), size * 0.46f);
    p->setFillStyle(gradient1);
    p->fill();
    p->setStrokeStyle(QColor(0x202020));
    p->setLineWidth(size * 0.02f);
    p->stroke();

이 예제에서 사용하는 기능에 대한 자세한 내용은 QCanvasPainter, QCanvasBrush, QCanvasRadialGradient, QCanvasImagePattern, QCanvasImage, QFont 을 참조하세요.

이미지는 하트 모양을 채우기 위한 패턴으로 사용됩니다:

    // Paint heart
    QCanvasImagePattern pattern(m_image, centerX, centerY, size * 0.08f, size * 0.05f);
    p->setFillStyle(pattern);

QCanvasImageQCanvasOffscreenCanvas 과 같은 리소스가 관련된 경우 initializeResources() 및 graphicsResourcesInvalidated()에서 관리됩니다:

void CanvasWidget::initializeResources(QCanvasPainter *p)
{
    Q_ASSERT(m_image.isNull());
    const auto flags = QCanvasPainter::ImageFlag::Repeat | QCanvasPainter::ImageFlag::GenerateMipmaps;
    m_image = p->addImage(QImage(u":/qt-translucent.png"_s), flags);
}

void CanvasWidget::graphicsResourcesInvalidated()
{
    m_image = {};
}

초기화자원()은 단지 편의를 위한 것입니다. 이를 구현하는 대신 paint()에 다음과 같이 작성할 수도 있습니다:

if (m_image.isNull())
    m_image = p->addImage(QImage(":/qt-translucent.png"), QCanvasPainter::ImageFlag::Repeat);

이 예제에서는 창 간에 위젯을 다시 부모로 지정하지 않으므로 그래픽 리소스가 손실되지 않습니다. 그럼에도 불구하고 그래픽스 리소스의 모든 QCanvasImageQCanvasOffscreenCanvas 변수에 기본 빈 객체를 할당하는 것이 좋은 패턴입니다.

main() 함수는 QMainWindowQMdiArea 을 생성합니다. 캔버스위젯 클래스의 여러 인스턴스를 하위 창으로 추가할 수 있습니다. hasSharedPainter ()의 기본값이 true이고 동일한 최상위 위젯 내에 배치되기 때문에 모든 페인터 위젯은 전용 위젯을 만드는 대신 동일한 QCanvasPainter 및 관련 렌더링 인프라를 공유합니다.

예제 프로젝트 @ code.qt.io

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.