QCanvasPainterWidget Class
QCanvasPainterWidget は、QCanvasPainter を使用してレンダリングするためのウィジェットです。詳細...
| ヘッダー | #include <QCanvasPainterWidget> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS CanvasPainter)target_link_libraries(mytarget PRIVATE Qt6::CanvasPainter) |
| 以来: | Qt 6.11 |
| を継承しています: | QRhiWidget |
| ステータス | 技術プレビュー |
パブリック関数
| QCanvasPainterWidget(QWidget *parent = nullptr) | |
| virtual | ~QCanvasPainterWidget() override |
| QColor | fillColor() const |
| void | grabCanvas(const QCanvasOffscreenCanvas &canvas, std::function<void (const QImage &)> callback) |
| bool | hasSharedPainter() const |
| void | setFillColor(const QColor &color) |
| void | setSharedPainter(bool enable) |
保護された関数
| void | beginCanvasPainting(QCanvasOffscreenCanvas &canvas) |
| void | endCanvasPainting() |
| virtual void | graphicsResourcesInvalidated() |
| virtual void | initializeResources(QCanvasPainter *painter) |
| virtual void | paint(QCanvasPainter *painter) |
| virtual void | prePaint(QCanvasPainter *painter) |
再実装された保護された関数
| virtual void | initialize(QRhiCommandBuffer *cb) override |
| virtual void | releaseResources() override |
| virtual void | render(QRhiCommandBuffer *cb) override |
詳細説明
QCanvasPainter を使用してレンダリングを実行するために、paint 仮想関数をサブクラスで実装します。
以下のコード・スニペットは、QCanvasPainterWidgetサブクラスの典型的な構造を示しています:
class MyWidget : public QCanvasPainterWidget { public: void initializeResources(QCanvasPainter *p) override { // load assets if (m_image.isNull()) m_image = p->addImage(QImage("image.png"), QCanvasPainter::ImageFlag::Repeat); } void paint(QCanvasPainter *p) override { // ... draw using m_image } void graphicsResourcesInvalidated() override { // textures are lost, indicate the need for reload m_image = {}; } QCanvasImage m_image; };
メンバ関数ドキュメント
[explicit] QCanvasPainterWidget::QCanvasPainterWidget(QWidget *parent = nullptr)
与えられたparent で QCanvasPainterWidget を構築します。
[override virtual noexcept] QCanvasPainterWidget::~QCanvasPainterWidget()
QCanvasPainterWidget を破壊する。
[protected] void QCanvasPainterWidget::beginCanvasPainting(QCanvasOffscreenCanvas &canvas)
canvas をターゲットとしたQCanvasPainter 描画コマンドの記録を開始する。
注意: この関数は、prePaint() からのみ呼び出す必要があります。
beginCanvasPainting()の後には、prePaint()から戻る前に、必ず対応するendCanvasPainting()を記述する必要があります。
QCanvasPainterWidget サブクラスの次のスニペットは、ウィジェットのコンテンツを描画するときに、オフスクリーン キャンバスをどのようにレンダリングし、画像または画像パターンとして使用するかを示しています:
class MyWidget : public QCanvasPainterWidget { public: QCanvasOffscreenCanvas canvas; QCanvasImage canvasImage; void graphicsResourcesInvalidated() override { canvas = {}; // so that the next prePaint() will recreate and redraw the canvas } void prePaint(QCanvasPainter *p) override { if (canvas.isNull()) { canvas = p->createCanvas(QSize(320, 240)); beginCanvasPainting(canvas); p->beginPath(); p->circle(160, 120, 20); p->setFillStyle(Qt::red); p->fill(); endCanvasPainting(); canvasImage = p->addImage(canvas, QCanvasPainter::ImageFlag::Repeat); } } void paint(QCanvasPainter *p) override { // use canvasImage as a brush or with drawImage() } };
[protected] void QCanvasPainterWidget::endCanvasPainting()
beginCanvasPainting() で指定したキャンバスを対象とした描画の終了を示します。
注意: この関数は、prePaint() からのみ呼び出す必要があります。
beginCanvasPainting(prePaint() から戻る前に、必ず対応する endCanvasPainting() を呼び出す必要があります。)
QColor QCanvasPainterWidget::fillColor() const
現在の塗りつぶし色を返します。
setFillColor()も参照 ください。
void QCanvasPainterWidget::grabCanvas(const QCanvasOffscreenCanvas &canvas, std::function<void (const QImage &)> callback)
canvas に対してテクスチャのリードバック要求を発行します。
callback この関数は、QRhi と 3D API の実装に応じて、関数が戻る前に呼び出されるか、後に呼び出されます。テクスチャコンテンツのリードバックは、GPU アーキテクチャによっては GPU->CPU コピーを伴うことがあります。
[virtual protected] void QCanvasPainterWidget::graphicsResourcesInvalidated()
テクスチャなどの基礎となるグラフィックリソースが失われたときに呼び出される。
これは、addImage()から返されたQCanvasImage オブジェクトがもはや有効ではなく、addImage()を再度呼び出す必要があることを示す。画像が使用されない、あるいは addImage() が常に呼び出されるなど、paint() の実装が問題にならない場合は、何もする必要はない。そうでない場合は、フラグをトグルするなどして、paint() の次の呼び出しで対応することを推奨する。
QCanvasPainter::createCanvas() から返されるQCanvasOffscreenCanvas オブジェクトについても同様である。この関数が呼び出された場合、paint ()の次の呼び出しでは、新しいキャンバスを作成し、その内容を再描画する必要があります。
例えば、ウィジェットが新しいトップレベル・ウィンドウに移動すると、新しいQRhi インスタンスと関連付けられることになるため、グラフィックス・リソースが失われる可能性がある。
QRhiWidget::releaseResources()も参照してください 。
bool QCanvasPainterWidget::hasSharedPainter() const
このウィジェットが共有ペインターを使用している場合はtrue を返します。
setSharedPainterも参照してください 。
[override virtual protected] void QCanvasPainterWidget::initialize(QRhiCommandBuffer *cb)
再実装:QRhiWidget::initialize(QRhiCommandBuffer *cb)。
[virtual protected] void QCanvasPainterWidget::initializeResources(QCanvasPainter *painter)
painter を使ってリソースを初期化するには、このメソッドを再実装する。一般に、このメソッドは最初のprePaint() とpaint() の前に一度だけ呼び出される。例外は、グラフィック・リソースが失われた場合です。graphicsResourcesInvalidated() を参照してください。その場合、このメソッドはその後に再度呼び出されます。
デフォルトの実装は空です。
[virtual protected] void QCanvasPainterWidget::paint(QCanvasPainter *painter)
painter を使ってペイントするために、このメソッドを再実装する。
ウィジェットはまずfillColor() で塗りつぶされる。
デフォルトの実装は空です。
[virtual protected] void QCanvasPainterWidget::prePaint(QCanvasPainter *painter)
この関数を再実装して、painter を使用して、1つ以上のオフスクリーンキャンバスに描画を行います。
デフォルトの実装は空です。
beginCanvasPainting() およびendCanvasPainting()も参照して ください。
[override virtual protected] void QCanvasPainterWidget::releaseResources()
再インプリメント:QRhiWidget::releaseResources().
[override virtual protected] void QCanvasPainterWidget::render(QRhiCommandBuffer *cb)
再実装:QRhiWidget::render(QRhiCommandBuffer *cb)。
void QCanvasPainterWidget::setFillColor(const QColor &color)
塗りつぶし色をcolor に設定します。 この色はアイテムの背景を描画するために使用されます。デフォルトは黒です。
fillColor()も参照してください 。
void QCanvasPainterWidget::setSharedPainter(bool enable)
enable がfalse の場合、ペインター共有を無効にする。
ペインター共有が有効な場合、同じQWindow 内のすべてのQCanvasPainterWidget インスタンスは、同じQCanvasPainter を使用します。この関数は、派生クラスのコンストラクタなどで初期化する必要があります。
ペインターの共有はデフォルトで有効です。
2つのウィジェットが専用の非共有ペインターを使用する場合、QCanvasImage や QOffscreenCanvas をバッキングしているものなど、お互いのグラフィックリソースは表示されません。一方、ウィジェットが同じウィンドウ内にあり、共有が有効になっている場合、どちらも同じQCanvasPainter を使用しているため、もう一方のウィジェットが作成した画像やキャンバスを使用することができます。
注意: enable が true の場合でも、異なるウィンドウに属するウィジェット (トップレベルのウィジェット) 間でペインターは共有されません。
hasSharedPainterも参照してください .
© 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.