このページでは

ペインターパスの例

「ペインターパス」のサンプルでは、ペインターパスを使用してレンダリング用の複雑な形状を構築する方法を示しています。

さまざまな形状やグラフィックオプションを表示するアプリケーション

QPainterPath クラスは、描画操作のためのコンテナを提供し、グラフィカルな図形の構築と再利用を可能にします。

ペインターパスは、多数のグラフィック構成要素(長方形、楕円、直線、曲線など)で構成されるオブジェクトであり、塗りつぶし、輪郭描画、クリッピングに使用できます。 通常の描画操作に比べてペインターパスの主な利点は、複雑な形状を一度作成するだけで済み、QPainter::drawPath() を呼び出すだけで何度でも描画できることです。

この例は 2 つのクラスで構成されています:

  • RenderArea クラス:単一のペインターパスを表示するカスタムウィジェットです。
  • Window クラス。これは、複数の `RenderArea ` ウィジェットを表示するアプリケーションのメインウィンドウであり、ユーザーがペインターパスの塗りつぶし、ペン、色、回転角度を操作できるようにします。

まず、Window クラスについて確認し、その後、RenderArea クラスについて見ていきます。

Windowクラスの定義

Window クラスはQWidget を継承しており、複数のRenderArea ウィジェットを表示し、ユーザーがペインターパスの塗りつぶし、ペン、色、回転角度を操作できるようにするアプリケーションのメインウィンドウです。

class Window : public QWidget
{
    Q_OBJECT

public:
    Window();

private slots:
    void fillRuleChanged();
    void fillGradientChanged();
    void penColorChanged();

塗りつぶしや色に関するユーザー入力に対応するため、3つのプライベートスロットを宣言します:fillRuleChanged() 、fillGradientChanged() 、およびpenColorChanged() です。

ユーザーがペンの太さや回転角度を変更すると、その新しい値はQSpinBox::valueChanged() シグナルを介して、RenderArea ウィジェットに直接渡されます。 塗りつぶしや色を更新するためにスロットを実装しなければならない理由は、QComboBox が新しい値を引数として渡すようなシグナルを提供していないためです。そのため、RenderArea ウィジェットを更新する前に、新しい値(または複数の値)を取得する必要があります。

private:
    void populateWithColors(QComboBox *comboBox);
    QVariant currentItemData(QComboBox *comboBox);

また、いくつかのプライベートな便利関数も宣言しています。populateWithColors() は、指定された `QComboBox ` に、Qt が認識する色名に対応する項目を格納し、currentItemData() は、指定された `QComboBox` の現在の項目を返します。

    QList<RenderArea*> renderAreas;
    QLabel *fillRuleLabel;
    QLabel *fillGradientLabel;
    QLabel *fillToLabel;
    QLabel *penWidthLabel;
    QLabel *penColorLabel;
    QLabel *rotationAngleLabel;
    QComboBox *fillRuleComboBox;
    QComboBox *fillColor1ComboBox;
    QComboBox *fillColor2ComboBox;
    QSpinBox *penWidthSpinBox;
    QComboBox *penColorComboBox;
    QSpinBox *rotationAngleSpinBox;
};

次に、メインウィンドウウィジェットの各種コンポーネントを宣言します。また、RenderArea ウィジェットの数を指定する利便性のための定数も宣言します。

Windowクラスの実装

Window のコンストラクタでは、さまざまなペインターパスを定義し、グラフィカルな形状を描画する対応するRenderArea ウィジェットを作成します。

Window::Window()
{
    QPainterPath rectPath;
    rectPath.moveTo(20.0, 30.0);
    rectPath.lineTo(80.0, 30.0);
    rectPath.lineTo(80.0, 70.0);
    rectPath.lineTo(20.0, 70.0);
    rectPath.closeSubpath();

QPainterPath::moveTo() およびQPainterPath::lineTo() 関数を使用して、角が鋭い長方形を作成します。

QPainterPath::moveTo() は、現在の描画ポイントを引数として渡された点に移動させます。ペインターパスとは、多数のグラフィカルな構成要素、すなわちサブパスから成るオブジェクトです。現在の描画ポイントを移動すると、新しいサブパスが開始されます(新しいパスが開始されると、以前のパスは暗黙的に閉じられます)。QPainterPath::lineTo() 関数は、現在の点から指定された終点まで直線を追加します。直線が描画された後、現在の点は直線の終点に更新されます。

まず、現在の点を移動して新しいサブパスを開始し、長方形の3つの辺を描画します。次に、QPainterPath::closeSubpath() 関数を呼び出し、現在のサブパスの開始点まで線を引きます。現在のサブパスが閉じられると、新しいサブパスが自動的に開始されます。新しいパスの現在の点は (0, 0) です。 また、QPainterPath::lineTo() を呼び出して最後の線も描画し、その後QPainterPath::moveTo() 関数を使用して明示的に新しいサブパスを開始することも可能です。

QPainterPath また、QPainterPath::addRect() という便利な関数も用意されており、指定された矩形を閉じたサブパスとしてパスに追加します。矩形は時計回りの線の集合として追加されます。rectが追加された後のペイントパスの現在の位置は、矩形の左上隅になります。

    QPainterPath roundRectPath;
    roundRectPath.moveTo(80.0, 35.0);
    roundRectPath.arcTo(70.0, 30.0, 10.0, 10.0, 0.0, 90.0);
    roundRectPath.lineTo(25.0, 30.0);
    roundRectPath.arcTo(20.0, 30.0, 10.0, 10.0, 90.0, 90.0);
    roundRectPath.lineTo(20.0, 65.0);
    roundRectPath.arcTo(20.0, 60.0, 10.0, 10.0, 180.0, 90.0);
    roundRectPath.lineTo(75.0, 70.0);
    roundRectPath.arcTo(70.0, 60.0, 10.0, 10.0, 270.0, 90.0);
    roundRectPath.closeSubpath();

次に、角が丸い矩形を作成します。前述と同様に、QPainterPath::moveTo() およびQPainterPath::lineTo() 関数を使用して矩形の辺を描画します。角を丸くするには、QPainterPath::arcTo() 関数を使用します。

QPainterPath::arcTo() は、指定された長方形(QRect または長方形の座標で指定)を範囲とし、指定された開始角度から反時計回りに指定された度数分だけ伸びる円弧を作成します。角度は度単位で指定します。 正の角度を指定することで、時計回りの円弧を作成することもできます。この関数は、現在の点と円弧の始点がまだ接続されていない場合、それらを接続します。

    QPainterPath ellipsePath;
    ellipsePath.moveTo(80.0, 50.0);
    ellipsePath.arcTo(20.0, 30.0, 60.0, 40.0, 0.0, 360.0);

また、楕円パスを構築するためにQPainterPath::arcTo() 関数も使用します。まず、新しいパスを開始するために現在の点を移動させます。次に、開始角度を 0.0、最後の引数を 360.0 度としてQPainterPath::arcTo() を呼び出し、楕円を作成します。

ここでも、QPainterPath は便利な関数(QPainterPath::addEllipse())を提供しており、指定された境界矩形内に楕円を作成し、それをペインターパスに追加します。現在のサブパスが閉じている場合は、新しいサブパスが開始されます。楕円は時計回りの曲線で構成され、0度(3時の位置)から始まり、0度で終わります。

    QPainterPath piePath;
    piePath.moveTo(50.0, 50.0);
    piePath.arcTo(20.0, 30.0, 60.0, 40.0, 60.0, 240.0);
    piePath.closeSubpath();

円グラフのパスを構築する際も、前述の関数の組み合わせを引き続き使用します。まず、現在のポイントを移動して新しいサブパスを開始します。次に、グラフの中心から円弧への線分と、円弧自体を作成します。サブパスを閉じる際、グラフの中心に戻る最後の線分が暗黙的に構築されます。

    QPainterPath polygonPath;
    polygonPath.moveTo(10.0, 80.0);
    polygonPath.lineTo(20.0, 10.0);
    polygonPath.lineTo(80.0, 30.0);
    polygonPath.lineTo(90.0, 70.0);
    polygonPath.closeSubpath();

多角形の描画は、長方形の描画と同等です。

QPainterPath また、QPainterPath::addPolygon() という便利な関数も用意されており、これにより指定された多角線を新しいサブパスとしてパスに追加できます。多角線が追加された後の現在の位置は、その多角線の最後の点になります。

    QPainterPath groupPath;
    groupPath.moveTo(60.0, 40.0);
    groupPath.arcTo(20.0, 20.0, 40.0, 40.0, 0.0, 360.0);
    groupPath.moveTo(40.0, 40.0);
    groupPath.lineTo(40.0, 80.0);
    groupPath.lineTo(80.0, 80.0);
    groupPath.lineTo(80.0, 40.0);
    groupPath.closeSubpath();

次に、サブパスのグループで構成されるパスを作成します。まず、現在のポイントを移動し、楕円パスを作成したときと同様に、QPainterPath::arcTo() 関数を使用して、開始角度を 0.0、最後の引数を 360 度に設定して円を作成します。 次に、現在のポイントを再度移動して新しいサブパスを開始し、`QPainterPath::lineTo()`関数を使用して正方形の3つの辺を作成します。

ここで、QPainterPath::closeSubpath() 関数を呼び出すと、最後の辺が作成されます。QPainterPath::closeSubpath() 関数は、現在のサブパスの先頭、つまり正方形まで線を引くことを覚えておいてください。

QPainterPath 利便性を高めるため、QPainterPath::addPath() という関数を用意します。これは、指定されたパスを、この関数を呼び出したパスの末尾に追加するものです。

    QPainterPath textPath;
    QFont timesFont("Times", 50);
    timesFont.setStyleStrategy(QFont::ForceOutline);
    textPath.addText(10, 70, timesFont, tr("Qt"));

テキストパスを作成する際は、まずフォントを作成します。次に、フォントのスタイル戦略を設定します。これは、フォントマッチングアルゴリズムに対して、適切なデフォルトファミリーを見つけるためにどの種類のフォントを使用すべきかを指示するものです。QFont::ForceOutline は、アウトラインフォントの使用を強制します。

テキストを構築するには、QPainterPath::addText() 関数を使用します。この関数は、指定されたテキストを、提供されたフォントから作成された一連の閉じたサブパスとしてパスに追加します。サブパスは、テキストのベースラインの左端が指定された点に位置するように配置されます。

    QPainterPath bezierPath;
    bezierPath.moveTo(20, 30);
    bezierPath.cubicTo(80, 0, 50, 50, 80, 80);

ベジェパスを生成するには、QPainterPath::cubicTo() 関数を使用します。この関数は、現在の点と指定された終点との間に、指定された制御点を持つベジェ曲線を追加します。曲線が追加されると、現在の点は曲線の終点に更新されます。

この例では、単純な曲線のみを作成するため、サブパスの閉鎖は省略しています。しかし、曲線の終点からサブパスの始点へと続く論理的な線は依然として存在しており、アプリケーションのメインウィンドウで確認できるように、パスを塗りつぶすとそれが目に見えるようになります。

    QPainterPath starPath;
    starPath.moveTo(90, 50);
    for (int i = 1; i < 5; ++i) {
        starPath.lineTo(50 + 40 * std::cos(0.8 * i * M_PI),
                        50 + 40 * std::sin(0.8 * i * M_PI));
    }
    starPath.closeSubpath();

最終的に構築されたパスは、前述のQPainterPath::moveTo()、QPainterPath::lineTo()、QPainterPath::closeSubpath()関数のみを使用して、QPainterPath でかなり複雑な形状を作成できることを示しています。

    renderAreas.push_back(new RenderArea(rectPath));
    renderAreas.push_back(new RenderArea(roundRectPath));
    renderAreas.push_back(new RenderArea(ellipsePath));
    renderAreas.push_back(new RenderArea(piePath));
    renderAreas.push_back(new RenderArea(polygonPath));
    renderAreas.push_back(new RenderArea(groupPath));
    renderAreas.push_back(new RenderArea(textPath));
    renderAreas.push_back(new RenderArea(bezierPath));
    renderAreas.push_back(new RenderArea(starPath));

必要なペインターパスをすべて作成したところで、それぞれに対応するRenderArea ウィジェットを作成します。最後に、Q_ASSERT()マクロを使用して、レンダリング領域の数が正しいことを確認します。

    fillRuleComboBox = new QComboBox;
    fillRuleComboBox->addItem(tr("Odd Even"), Qt::OddEvenFill);
    fillRuleComboBox->addItem(tr("Winding"), Qt::WindingFill);

    fillRuleLabel = new QLabel(tr("Fill &Rule:"));
    fillRuleLabel->setBuddy(fillRuleComboBox);

次に、ペインターパスの塗りつぶしルールに関連付けられたウィジェットを作成します。

Qt では 2 種類の塗りつぶしルールが利用可能です:Qt::OddEvenFill ルールは、点から図形の外側にある位置まで水平線を引いて、交点の数を数えることで、その点が図形の内側にあるかどうかを判定します。交点の数が奇数であれば、その点は図形の内側にあります。このルールがデフォルトです。

Qt::WindingFill ルールは、点から図形の外側の位置へ水平線を引いて、その線が各交点で上向きか下向きかを判定し、各交点の方向を合計して「巻き数」を算出します。この数値が0以外の場合、その点は図形の内側にあるとみなされます。

Qt::WindingFill ルールは、ほとんどの場合、閉じた図形の交点として考えることができます。

    fillColor1ComboBox = new QComboBox;
    populateWithColors(fillColor1ComboBox);
    fillColor1ComboBox->setCurrentIndex(fillColor1ComboBox->findText("mediumslateblue"));

    fillColor2ComboBox = new QComboBox;
    populateWithColors(fillColor2ComboBox);
    fillColor2ComboBox->setCurrentIndex(fillColor2ComboBox->findText("cornsilk"));

    fillGradientLabel = new QLabel(tr("&Fill Gradient:"));
    fillGradientLabel->setBuddy(fillColor1ComboBox);

    fillToLabel = new QLabel(tr("to"));
    fillToLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    penWidthSpinBox = new QSpinBox;
    penWidthSpinBox->setRange(0, 20);

    penWidthLabel = new QLabel(tr("&Pen Width:"));
    penWidthLabel->setBuddy(penWidthSpinBox);

    penColorComboBox = new QComboBox;
    populateWithColors(penColorComboBox);
    penColorComboBox->setCurrentIndex(penColorComboBox->findText("darkslateblue"));

    penColorLabel = new QLabel(tr("Pen &Color:"));
    penColorLabel->setBuddy(penColorComboBox);

    rotationAngleSpinBox = new QSpinBox;
    rotationAngleSpinBox->setRange(0, 359);
    rotationAngleSpinBox->setWrapping(true);
    rotationAngleSpinBox->setSuffix(QLatin1String("\xB0"));

    rotationAngleLabel = new QLabel(tr("&Rotation Angle:"));
    rotationAngleLabel->setBuddy(rotationAngleSpinBox);

また、塗りつぶし、ペン、回転角に関連する他のウィジェットも作成します。

    connect(fillRuleComboBox, &QComboBox::activated,
            this, &Window::fillRuleChanged);
    connect(fillColor1ComboBox, &QComboBox::activated,
            this, &Window::fillGradientChanged);
    connect(fillColor2ComboBox, &QComboBox::activated,
            this, &Window::fillGradientChanged);
    connect(penColorComboBox, &QComboBox::activated,
            this, &Window::penColorChanged);

    for (RenderArea *area : std::as_const(renderAreas)) {
        connect(penWidthSpinBox, &QSpinBox::valueChanged,
                area, &RenderArea::setPenWidth);
        connect(rotationAngleSpinBox, &QSpinBox::valueChanged,
                area, &RenderArea::setRotationAngle);
    }

コンボボックスのactivated()シグナルをWindow クラスの関連するスロットに接続し、スピンボックスのvalueChanged()シグナルをRenderArea ウィジェットのそれぞれのスロットに直接接続します。

    QGridLayout *topLayout = new QGridLayout;

    int i = 0;
    for (RenderArea *area : std::as_const(renderAreas)) {
        topLayout->addWidget(area, i / 3, i % 3);
        ++i;
    }

    QGridLayout *mainLayout = new QGridLayout;
    mainLayout->addLayout(topLayout, 0, 0, 1, 4);
    mainLayout->addWidget(fillRuleLabel, 1, 0);
    mainLayout->addWidget(fillRuleComboBox, 1, 1, 1, 3);
    mainLayout->addWidget(fillGradientLabel, 2, 0);
    mainLayout->addWidget(fillColor1ComboBox, 2, 1);
    mainLayout->addWidget(fillToLabel, 2, 2);
    mainLayout->addWidget(fillColor2ComboBox, 2, 3);
    mainLayout->addWidget(penWidthLabel, 3, 0);
    mainLayout->addWidget(penWidthSpinBox, 3, 1, 1, 3);
    mainLayout->addWidget(penColorLabel, 4, 0);
    mainLayout->addWidget(penColorComboBox, 4, 1, 1, 3);
    mainLayout->addWidget(rotationAngleLabel, 5, 0);
    mainLayout->addWidget(rotationAngleSpinBox, 5, 1, 1, 3);
    setLayout(mainLayout);

RenderArea ウィジェットを別のレイアウトに追加し、それを他のウィジェットとともにメインレイアウトに追加します。

    fillRuleChanged();
    fillGradientChanged();
    penColorChanged();
    penWidthSpinBox->setValue(2);

    setWindowTitle(tr("Painter Paths"));
}

最後に、fillRuleChanged() 、fillGradientChanged() 、penColorChanged() のスロットを呼び出してRenderArea ウィジェットを初期化し、初期のペン幅とウィンドウのタイトルを設定します。

void Window::fillRuleChanged()
{
    Qt::FillRule rule = (Qt::FillRule)currentItemData(fillRuleComboBox).toInt();

    for (RenderArea *area : std::as_const(renderAreas))
        area->setFillRule(rule);
}

void Window::fillGradientChanged()
{
    QColor color1 = qvariant_cast<QColor>(currentItemData(fillColor1ComboBox));
    QColor color2 = qvariant_cast<QColor>(currentItemData(fillColor2ComboBox));

    for (RenderArea *area : std::as_const(renderAreas))
        area->setFillGradient(color1, color2);
}

void Window::penColorChanged()
{
    QColor color = qvariant_cast<QColor>(currentItemData(penColorComboBox));

    for (RenderArea *area : std::as_const(renderAreas))
        area->setPenColor(color);
}

プライベートスロットは、関連付けられたコンボボックスから新しい値(1つまたは複数)を取得し、RenderAreaウィジェットを更新するために実装されています。

まず、プライベート関数 `currentItemData() ` およびテンプレート関数 `qvariant_cast()` を使用して、新しい値(1つまたは複数)を特定します。次に、各 `RenderArea ` ウィジェットに関連付けられたスロットを呼び出し、ペインターパスを更新します。

void Window::populateWithColors(QComboBox *comboBox)
{
    const QStringList colorNames = QColor::colorNames();
    for (const QString &name : colorNames)
        comboBox->addItem(name, QColor(name));
}

populateWithColors() 関数は、静的関数QColor::colorNames()によって提供される、Qtが認識する色名に対応する項目を、指定されたコンボボックスに設定します。

QVariant Window::currentItemData(QComboBox *comboBox)
{
    return comboBox->itemData(comboBox->currentIndex());
}

currentItemData() 関数は、指定されたコンボボックスの現在の項目を単に返すだけです。

RenderArea クラスの定義

RenderArea クラスはQWidget を継承しており、単一のペインターパスを表示するカスタムウィジェットです。

class RenderArea : public QWidget
{
    Q_OBJECT

public:
    explicit RenderArea(const QPainterPath &path, QWidget *parent = nullptr);

    QSize minimumSizeHint() const override;
    QSize sizeHint() const override;

public slots:
    void setFillRule(Qt::FillRule rule);
    void setFillGradient(const QColor &color1, const QColor &color2);
    void setPenWidth(int width);
    void setPenColor(const QColor &color);
    void setRotationAngle(int degrees);

protected:
    void paintEvent(QPaintEvent *event) override;

RenderArea ウィジェットに関連付けられたペインターパスを更新する、いくつかのパブリックスロットを宣言します。さらに、QWidget::minimumSizeHint() およびQWidget::sizeHint() 関数を再実装して、RenderArea ウィジェットがアプリケーション内で適切なサイズになるようにし、QWidget::paintEvent() イベントハンドラを再実装して、そのペインターパスを描画します。

private:
    QPainterPath path;
    QColor fillColor1;
    QColor fillColor2;
    int penWidth;
    QColor penColor;
    int rotationAngle;
};

RenderArea クラスの各インスタンスには、QPainterPath 、いくつかの塗りつぶし色、ペンの幅、ペンの色、および回転角が設定されています。

RenderArea クラスの実装

コンストラクタは、QPainterPath を引数として受け取ります(オプションの親であるQWidget に加えて):

RenderArea::RenderArea(const QPainterPath &path, QWidget *parent)
    : QWidget(parent), path(path)
{
    penWidth = 1;
    rotationAngle = 0;
    setBackgroundRole(QPalette::Base);
}

コンストラクタ内では、QPainterPath パラメータを使用してRenderArea ウィジェットを初期化するほか、ペン幅と回転角度も初期化します。また、ウィジェットのbackground role を設定します。QPalette::Base は通常、白色に設定されます。

QSize RenderArea::minimumSizeHint() const
{
    return QSize(50, 50);
}

QSize RenderArea::sizeHint() const
{
    return QSize(100, 100);
}

次に、QWidget::minimumSizeHint() およびQWidget::sizeHint() 関数を再実装し、RenderArea ウィジェットがアプリケーション内で適切なサイズになるようにします。

void RenderArea::setFillRule(Qt::FillRule rule)
{
    path.setFillRule(rule);
    update();
}

void RenderArea::setFillGradient(const QColor &color1, const QColor &color2)
{
    fillColor1 = color1;
    fillColor2 = color2;
    update();
}

void RenderArea::setPenWidth(int width)
{
    penWidth = width;
    update();
}

void RenderArea::setPenColor(const QColor &color)
{
    penColor = color;
    update();
}

void RenderArea::setRotationAngle(int degrees)
{
    rotationAngle = degrees;
    update();
}

さまざまなパブリックスロットは、関連するプロパティを設定してRenderArea ウィジェットのペインタパスを更新し、QWidget::update() 関数を呼び出して、新しいレンダリング設定でウィジェットの再描画を強制します。

QWidget::update() スロットは、即座に再描画を行うのではなく、Qt がメインイベントループに戻った際に処理されるよう、ペイントイベントをスケジュールします。

void RenderArea::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

ペイントイベントとは、ウィジェットの全部または一部を再描画する要求のことです。paintEvent() 関数は、ウィジェットのペイントイベントを受け取るために再実装可能なイベントハンドラです。ここでは、RenderArea ウィジェットのペインターパスをレンダリングするために、このイベントハンドラを再実装します。

まず、RenderArea インスタンス用のQPainter を作成し、ペインターのレンダリングヒントを設定します。QPainter::RenderHints は、QPainter に対するフラグを指定するために使用され、特定のエンジンによって尊重される場合とされない場合があります。QPainter::Antialiasing は、可能であればエンジンがプリミティブのエッジにアンチエイリアス処理を行うべきであることを示します。つまり、エッジを滑らかにするために、元のピクセルの周囲に追加のピクセルを配置することを意味します。

    painter.scale(width() / 100.0, height() / 100.0);
    painter.translate(50.0, 50.0);
    painter.rotate(-rotationAngle);
    painter.translate(-50.0, -50.0);

次に、QPainter の座標系をスケーリングし、ペインターパスが適切なサイズでレンダリングされるようにします。つまり、アプリケーションのサイズ変更時に、RenderArea ウィジェットに合わせてペインターパスも拡大されるようにします。 さまざまなペインターパスを構築した際、それらはすべて、RenderArea::sizeHint() に相当する幅100ピクセルの正方形内にレンダリングされていました。QPainter::scale()関数は、RenderArea ウィジェットの現在の幅と高さを100で割った値で座標系をスケーリングします。

これで、ペインターパスのサイズが適切であることを確認できたので、座標系を平行移動させて、ペインターパスがRenderArea ウィジェットの中心を軸に回転するようにします。回転処理を行った後は、座標系を元の位置に戻すことを忘れないようにしてください。

    painter.setPen(QPen(penColor, penWidth, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
    QLinearGradient gradient(0, 0, 0, 100);
    gradient.setColorAt(0.0, fillColor1);
    gradient.setColorAt(1.0, fillColor2);
    painter.setBrush(gradient);
    painter.drawPath(path);
}

次に、QPainter のペンをインスタンスのレンダリング設定に合わせて設定します。QLinearGradient を作成し、その色をRenderArea ウィジェットの塗りつぶし色に合わせて設定します。最後に、QPainter のブラシを設定し(グラデーションは自動的にQBrush に変換されます)、QPainter::drawPath()関数を使用してRenderArea ウィジェットのペインターパスを描画します。

サンプルプロジェクト @ 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.