PDA

View Full Version : problem rendering garbled when rendering twice (QGraphicsScene->render(..))



alwin
1st December 2009, 16:09
I am having problems rendering a scene for the second time. The first time I do a QGraphicsScene->render(...) it renders ok and I am able to save the QImage as a png file. However when I try the same again on the same scene but a different portion(using a new QPainter and a new QImage) I get partly garbled rendering. (a lot of horizontal stripes through the picture)

This is the piece of test code that does the rendering and creates an image. The second image created contains the garbled picture. (Also when I call this routine again both images get garbled)



QImage *img;
QPainter *paint;

img = new QImage(QSize(960, 600), QImage::Format_ARGB32);
paint = new QPainter(img);
itsGraphicResourceScene->render(paint, QRectF(), QRectF(3070, 0, 960, 600));
paint->end();
img->save("test1.png", "png");
delete img;
delete paint;

img = new QImage(QSize(960, 600), QImage::Format_ARGB32);
paint = new QPainter(img);
itsGraphicResourceScene->render(paint, QRectF(), QRectF(3910, 0, 960, 600));
paint->end();
img->save("test2.png", "png");
delete img;
delete paint;


I have even tried to completely refresh the scene by using QGrahicsScene->clear() and then adding all graphic items again. With the same results. Also, I have tried creating a new scene and added the graphic items to it and rendered from that scene (with the same results...)

Unfortunately I am not able to add a screenshot because the forum only accepts an URL when inserting a image :(

wysota
1st December 2009, 16:46
Unfortunately I am not able to add a screenshot because the forum only accepts an URL when inserting a image :(

You can add an attachment containing the image. Also please provide a minimal compilable example reproducing the problem.

scascio
1st December 2009, 17:53
Since the only difference I see in the second part of your code is the rect provided in render(), did you try to call it with same coordinates?

Then we will know if the problem is related to coordinates of if there is a mistake elsewhere.

alwin
2nd December 2009, 09:14
Thanks both for your suggestions. I will try to use the same coordinates first, although I don't think that will help, because when I call this routine again it also garbles up the first image which in the first run is ok. I will try anyway and post results. After that I will try to reproduce the problem using minimal code. Maybe then I will even find the problem.

I am using my own class GraphicTask which is derived from QGraphicsItem.
In that class I override boundingRect(), paint(), mousePressEvent(), and mouseReleaseEvent(). Could that have anything to do with it? I will post the minimal example ASAP.

I attached the two images test1.png and test2.png. test1.png is ok (first run) and test2.png is garbled as you will notice. I also noticed that the second image always has about three times the byte size as the first. This could be an important clue, because the images are not that different.
The attached images are not the original images, I had to flatten them in the GIMP because the where transparant and in their original form not displayed correctly after uploading. (side question: How do I loose the transparancy in QT? I don't need nor want it)

Thanks both for your suggestions so far.

alwin
2nd December 2009, 09:50
I created another image, because I suspected it had something to do with the graphicTask objects (the green and blue rectangles). Some of these objects are partly in the rendered picture and partly outside it. (i.e. boundingRect() is partly outside the image) I shifted these task objects and recreated the image. Now I see that some of the timeline of the first render is also in the second image. This is strange because I use a newly created image (see the code) and render to that.
I also created an image without any task obejcts (only the timeline and the night-time rectangles (the grey rectangles) are in the image. When I do this it does not garble up the second image (see attached test2.jpg)

wysota
2nd December 2009, 10:15
So how about that example code?

Bitto
2nd December 2009, 22:10
Make sure to initialize the image. By default it's full of uninitialized pixels, and QGraphicsScene::render() will blend on top (source-over composition). When your app starts, chances are when you create the first image, it's going to be blank because the OS blanks those bits. Second time however, the image is likely to contain junk from the last time you rendered.

See QImage::fill(). Don't know if that will solve the problem but it's worth a shot...

alwin
3rd December 2009, 17:42
After two days, I found the problem! It wasn't in the software (at least not in my software). I remembered seeing such garbled stripes once more in another application under Linux. The laptop I use is quite new and uncommon that its display driver for linux is probably not widely tested. However I suspected that the driver was the problem and compiled the software under windows. And surely the problem was gone! It seems the display driver for my DELL Latitude E6400 is the problem.
Anyway, thanks folks for your replies!

panly099
7th November 2012, 20:20
Make sure to initialize the image. By default it's full of uninitialized pixels, and QGraphicsScene::render() will blend on top (source-over composition). When your app starts, chances are when you create the first image, it's going to be blank because the OS blanks those bits. Second time however, the image is likely to contain junk from the last time you rendered.

See QImage::fill(). Don't know if that will solve the problem but it's worth a shot...

This explanation is the right point and helps me out of my struggle. Thanks Bitto.