그래서 여기에 제가하려고하는 것이 있습니다 - 커스텀 QGraphicsItem을 사용하면 QImage에 그림을 그리는 QPainter 설정이 있습니다. 필요할 때까지 QImage를 메모리에 보관하십시오).씬/뷰를 필요로하지 않고 QImage에 QGraphicsItem을 그립니다.
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();
....
이 모두 잘 작동하지만 그것이 MainWindow를에 표시하는 것처럼 내가 필요한보기/장면을 원하지 않는다 : 여기
는 테스트 목적으로 내 프로젝트 외부 코드입니다 이 주위에?
나는 분명히 그 순간이 있었어. 나는 이미 다른 것을위한 장면/전망을 가지고있다. QGraphicsItem을 보유하기 위해이를 활용할 수 있습니다. 그러나 나는 누군가에게 내가하고있는 일에 대한 더 나은 제안을하거나 QGraphicsItem에서 QItem을 저장하고 장면 /보기를 필요로하지 않도록 다른 사람이 만날 수있는 원래의 질문에 대답 할 수 있도록이 공개 된 것을 남겨 둘 것이다. 대화 상자/주 창에 표시됩니다. – giraffee
왜 다른 곳에 페인트하지 않으시겠습니까? –
다시 생각한 후에도 맞춤 QGraphicsItem이 필요하지 않습니다. QImage에 페인트 만하려는 이유는 QCursor에서 사용할 QPixmap으로 변환하기 위해서입니다. – giraffee