PDA

View Full Version : QGraphicsView Render() Issue



paul567
14th November 2007, 01:20
I'm working on building an application to plot data. This application is being built to work on Windows and Linux. The plot is displayed using a QGraphicsView.

In Linux when we print the QGraphicsView using render() it comes out fine, but in Windows the area displayed by the QGraphicsView comes out all black.

Does anyone have any ideas.

wysota
14th November 2007, 01:35
Can we see the code?

paul567
14th November 2007, 01:45
Here is the snippet that does prepares the GraphicsView for printing.



void GraphicsView::brender(QPainter* painter, const QRect& rect, const QRect& target)
{
// a little bit of trickery to get the plot to scale right when printed
pview_rect = QRectF(
rect.x() * 1000,
rect.y() * 1000,
rect.width() * 1000,
rect.height() * 1000
);

render(painter, target.translated((viewport()->rect().width() > target.width() ? (-(viewport()->rect().width() - target.width())/2) : 0), 0), rect);
pview_rect = QRectF();
}

wysota
14th November 2007, 01:59
What is the contents of the scene? What happens if you print an empty scene? I think your problem is caused by multiplying the rectangle by 1000. The printer on Windows and on Linux may have different characteristics. You should probably query for the device resolution or do the scaling some other way.

paul567
14th November 2007, 03:03
Sorry for being so vauge. I'm part of a team and while I didn't write this section of code I'm trying to debug it. If I knew the code back to front then I wouldn't be struggling trying to tell you what is going on.

I am sure though that the multiplying isn't having a negative effect because the resolution of the plot turns out perfect.

paul567
15th November 2007, 00:15
Ok I've done more research into this and I can actually elaborate on the issue now.

We have a child of a QGraphicsTextItem called Annotation. Annotation has a child of a QGraphicsLineItem called Line as a private attribute.

We can add the Annotation to the QGraphicsScene without a problem, both the Annotation and the Line both appear without any graphical errors when viewed on the QGraphicsView.

The problem comes when the QGraphicsView is rendered to a printer on Windows. While both the Annotation and the Line appear the entire area of the scene appears black EXCEPT for (what appears to be) the boundingRect of the Line.

I've examined the overloaded paint function of both Annotation and Line. It seems to be pretty straight forward, the printer's pen is set up and the line is drawn.



qreal lwidth = 1/(1000*option->levelOfDetail)*width; // required to multiple by 1000 to set correctly for the scene
QPen myPen = pen();
myPen.setWidth((int)lwidth);
painter->setPen( myPen );
setLine( get_line(widget,line()) );
painter->drawLine( line() );


I also found that if I were to call QGraphicLineItem::paint() within the overloaded function the Graphics View is printed without all the black, but there is a black line going around one edge of the boundingRect of the line.

I am sure that the problem is the Line because if I remove the code to add the Line no problems occur and the same problem does not occur with any of the other QGraphicsItems being added to the scene.

I hope I've been more through then I was with my past few posts.

wysota
15th November 2007, 00:22
You should really avoid multiplying by 1000 everywhere. And you seem to be using the level of detail incorrectly - using it to calculate the length of anything that is to be drawn seems to be an obvious logic error, for instance if you have a scale of "2", LOD will be "2" as well so lwidth will be 2000 and because the scale is "2", everything is multiplied by "2" and the total length of the line will be 4000. For scale of "1" it will be 1000 and for scale of "4" it will be 16000, etc.

paul567
15th November 2007, 18:36
The Plot when printed in Linux
http://img260.imageshack.us/img260/4578/goodplotqn2.th.png (http://img260.imageshack.us/my.php?image=goodplotqn2.png)

The Plot when printed in Windows
http://img260.imageshack.us/img260/3461/badplotaz4.th.png (http://img260.imageshack.us/my.php?image=badplotaz4.png)

The reason this is being done is because of the amount of detail put into the scene. I understand that it may seem strange ( and thus an obvious source of potential problems ) but I don't see why that would cause the GraphicsScene to be rendered all black.

I've posted images of what was being printed out to try and show what the problem actually is.

wysota
15th November 2007, 23:46
Ok, look - you can either provide some complete and real code used for rendering and allow us to examine it and try to isolate the problem or we can continue to chase each other and keep running in circles. Please provide code along with the version number of Qt you are using. Please also try printing the scene to pdf and rendering to an image and see if in all cases you get an incorrect output.

paul567
16th November 2007, 01:07
Ok here ya go:
http://files-upload.com/files/621536/plotplugin.tar.gz

Qt 4.3.2 and gcc 4.1.2

Once extracted the src dir builds the plugin, the demo_src dir then uses the plugin.

Once compiled and running you will see two plot areas, the top area uses an Annotation without an initialized Line while the bottom uses an Annotation
with an initialized Line.

If you click the big printer icon a print dialog appears allowing you to select a printer.

If you print in Linux the plot areas will print correctly, if you print in Windows the bottom area will appear black because it's scene has a Line added to it.

I haven't tried painting it to a pixmap or anything just yet but I will get on that and post my feedback.

paul567
16th November 2007, 01:37
I tested my code on having it paint to a pixmap instead of a printer and it comes out fine ( once I fill my pixmap with Qt::white before I paint stuff to it ).

wysota
16th November 2007, 11:08
I tested my code on having it paint to a pixmap instead of a printer and it comes out fine ( once I fill my pixmap with Qt::white before I paint stuff to it ).

This immediately suggests the problem is with different device characteristics. I'll have a look at code you provided and see what I can find. Just please, next time try to use internal capabilities of the forum to host images or code instead of using external sites.

wysota
16th November 2007, 13:26
What happens if you call QGraphicsScene::render insted of QGraphicsView::render?