1

これが私がやろうとしていることです-カスタムQGraphicsItemを使用して、QPainterをセットアップしてQImageにペイントし、それをファイルに保存します(または必要になるまでQImageをメモリに保持します)。

私が見つけた問題は、QGraphicsItem::paint() が呼び出されるのは、QGraphcsItem がシーンに属し、シーンがビューに属し、かつビューとシーンが非表示になっていない場合のみです。

テスト目的で私のプロジェクト外のコードを次に示します。

MyQGfx Class
void MyQGfx::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    qDebug() << "begin of paint function";
    QRectF rec = boundingRect();

    QImage image(boundingRect().size().toSize(),
                 QImage::Format_ARGB32_Premultiplied);
    image.fill(0);

    // construct a dedicated offline painter for this image
    QPainter imagePainter(&image);
    imagePainter.translate(-boundingRect().topLeft());

    // paint the item using imagePainter
    imagePainter.setPen(Qt::blue);
    imagePainter.setBrush(Qt::green);
    imagePainter.drawEllipse(-50, -50, 100, 100);

    imagePainter.end();


    if(image.save("C://plot.jpg"))
    {
        qDebug() << "written";
    }
    else {
        qDebug() << "not written";
    }
}

MainWindow Class
....
QGraphicsView* view = new QGraphicsView(this);
QGraphicsScene* scene = new QGraphicsScene(this);
view->setScene(scene);

MyQGfx* gfx = new MyQGfx();
scene->addItem(gfx);
gfx->update();
....

これはすべて正常に機能しますが、メインウィンドウに表示されるため、ビュー/シーンは必要ありません-これを回避する方法はありますか?

4

1 に答える 1

2

QPainter を受け入れるカスタム メソッドを作成することはできませんか?

于 2012-03-25T11:20:49.787 に答える