Reputation: 14615
Rendering to bitmap, I have to create a QPainter. If I have to render multiple areas to multiple bitmaps, how do I reset the QPainter ?
QImage img1(scene1.sceneRect().size().toSize(), QImage::Format_ARGB32_Premultiplied);
img1.fill(Qt::color0);
QPainter painter1(&img1);
painter1.setRenderHint(QPainter::Antialiasing);
scene1.render(&painter1);
painter1.end();
QImage img2(scene2.sceneRect().size().toSize(), QImage::Format_ARGB32_Premultiplied);
img2.fill(Qt::color0);
QPainter painter2(&img2);
painter2.setRenderHint(QPainter::Antialiasing);
scene2.render(&painter2);
painter2.end();
How can I reuse the painter ? am I getting performance hits/higher memory usage by creating a new QPainter for each scene / image ?
Trying
QImage img(scene1.sceneRect().size().toSize(), QImage::Format_ARGB32_Premultiplied);
img.fill(Qt::color0);
QPainter painter(&img);
painter.setRenderHint(QPainter::Antialiasing);
scene1.render(&painter);
painter.end();
img.save("img.png");
img = QImage(scene2.sceneRect().size().toSize(), QImage::Format_ARGB32_Premultiplied);
img.fill(Qt::color0);
painter = QPainter(&img);
painter.setRenderHint(QPainter::Antialiasing);
scene2.render(&painter);
painter.end();
I get errors of the type
error: 'QPainter& QPainter::operator=(const QPainter&)' is private
Upvotes: 1
Views: 1058
Reputation: 2985
You can reuse your QPainter
with the following syntax:
QPainter painter;
painter.begin( &img1 );
...
painter.begin( &img2 );
...
But you can only use one QPainter
for one QPaintDevice
(in this case an image).
Upvotes: 3
Reputation: 2985
I think you do not need to worry about the reusing of the QImage. You can instantiate a new instance and use the being( ... ) function of the QPainter class.
Upvotes: 0
Reputation: 2985
You do NOT need to write this:
QPainter* painter = new QPainter( &img );
you can write this instead:
QPainter painter( &img );
with this you don't have to think about the deleting of the obejct.
Yes, all the QObject derived classes have private copy constructors. You can read about the reasons here.
Upvotes: 0
Reputation: 14615
It appears that QObject and Classes derived from it, as well as QPainter and maybe other classes, have private constructors (??? why)
So...
QImage img(scene1.sceneRect().size().toSize(), QImage::Format_ARGB32_Premultiplied);
img.fill(Qt::color0);
QPainter* painter = new QPainter(&img);
painter->setRenderHint(QPainter::Antialiasing);
scene1.render(painter);
painter->end();
img.save("img.png");
img = QImage(scene2.sceneRect().size().toSize(), QImage::Format_ARGB32_Premultiplied);
img.fill(Qt::color0);
painter = new QPainter(&img);
painter->setRenderHint(QPainter::Antialiasing);
scene2.render(painter);
painter->end();
delete painter;
I was writing this as vizhanyolajos was posting his answer, and I think I prefer that answer.
Upvotes: 0