Unsolved How to correctly override paint in QGraphicsItem?
-
Hello. I need to rasterize QGraphicsItem. My solution for this, is to override paint method in QGraphicsItem. But what this function should do? For now, this method is not overrided but on output image I have random noise.
-
For now I Have
QImage image(item->rect().size().toSize(), QImage::Format_RGB32); image.fill(Qt::transparent); QPainter painter(&image); painter.setRenderHint(QPainter::Antialiasing); QStyleOptionGraphicsItem opt; item->paint(&painter, &opt, nullptr); QDialog dialog; QLabel label; label.setPixmap(QPixmap::fromImage(image)); label.setParent(&dialog); dialog.show(); dialog.exec();
But on the preview there is a black square rather than rasterized item.
Edit:void Item::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) { painter->drawRect(boundingRect()); }
-
Hi,
What exactly are you expecting ?
Your paint method just draws a rectangle with whatever configuration painter has. -
@SGaist said in How to correctly override paint in QGraphicsItem?:
Hi,
What exactly are you expecting ?
Your paint method just draws a rectangle with whatever configuration painter has.Yeah, I noticed that xD For now, i use this example: https://gist.github.com/dokinkon/4275597 but rendering area is not in local coordinates, but in scene coordinates. I want to render whole QGraphicsRectItem to the image without any tranformation. Do you know to do this?
-
nevermind. I save position and rotation at the beginning of this function, then set pos to (0, 0) and rotation to 0, render item and at the end – change it back to previous values xD
QImage Item::ToImage() { QPointF startpos = pos(); qreal startRotation = rotation(); setPos(0, 0); setRotation(0); QRectF r = rect(); QImage image(r.width(), r.height(), QImage::Format_RGB32); image.fill(QColor(0, 0, 0)); QPainter painter(&image); painter.drawRect(r); scene()->render(&painter, QRect(), sceneBoundingRect()); painter.end(); setPos(startpos); setRotation(startRotation); return image; }