Qt-jambi-interest Archive, December 2006
Render un QGraphicsScene on an image
Message 1 in thread
Hello,
I try to render a QGraphicsScene on an image. I have a function in my
QGraphicsView, named saveImage, which take a filename and save the scene :
public void svaeImage(String filename) {
QImage image = new QImage(width(), height(),
QImage.Format.Format_RGB32); // I tried Format_ARGB32
QPainter painter(image);
scene.render(painter);
painter.end(); // I tried with and without this line
image.save(filename, "JPEG");
}
But each time I call this function, I ended with a null image.
What did I do wrong ?
Thanks
Nicolas
Message 2 in thread
Nicolas Arnaud-Cormos wrote:
> Hello,
>
> I try to render a QGraphicsScene on an image. I have a function in my
> QGraphicsView, named saveImage, which take a filename and save the scene :
>
> public void svaeImage(String filename) {
> QImage image = new QImage(width(), height(),
> QImage.Format.Format_RGB32); // I tried Format_ARGB32
> QPainter painter(image);
> scene.render(painter);
> painter.end(); // I tried with and without this line
> image.save(filename, "JPEG");
> }
>
> But each time I call this function, I ended with a null image.
> What did I do wrong ?
The problem is that the jpeg plugin is not part of the Jambi package. Qt
has builtin support for PNG so this is the preferred image format,
partially because it also supports transparency.
See the attached example.
best regards,
Gunnar
package com.trolltech.tests;
package com.trolltech.tests;
import com.trolltech.qt.gui.*;
public class GraphicsViewSnapshot {
public static void main(String args[]) {
QApplication.initialize(args);
// Set up the scene..
QGraphicsScene scene = new QGraphicsScene();
QPainterPath path = new QPainterPath();
path.addText(0, 0, new QFont("Times New Roman", 40), "Qt Jambi");
QGraphicsPathItem item = scene.addPath(path, new QPen(QColor.red), new QBrush(QColor.black));
item.rotate(30);
// Create the image and render it...
QImage image = new QImage(400, 400, QImage.Format.Format_ARGB32_Premultiplied);
QPainter p = new QPainter(image);
p.setRenderHint(QPainter.RenderHint.Antialiasing);
scene.render(p);
p.end();
// Save it..
image.save("graphicsscene.png", "PNG");
}
}