Results 1 to 13 of 13

Thread: QGraphicsView Render() Issue

  1. #1
    Join Date
    Nov 2007
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QGraphicsView Render() Issue

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView Render() Issue

    Can we see the code?

  3. #3
    Join Date
    Nov 2007
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView Render() Issue

    Here is the snippet that does prepares the GraphicsView for printing.

    Qt Code:
    1. void GraphicsView::brender(QPainter* painter, const QRect& rect, const QRect& target)
    2. {
    3. // a little bit of trickery to get the plot to scale right when printed
    4. pview_rect = QRectF(
    5. rect.x() * 1000,
    6. rect.y() * 1000,
    7. rect.width() * 1000,
    8. rect.height() * 1000
    9. );
    10.  
    11. render(painter, target.translated((viewport()->rect().width() > target.width() ? (-(viewport()->rect().width() - target.width())/2) : 0), 0), rect);
    12. pview_rect = QRectF();
    13. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView Render() Issue

    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.

  5. #5
    Join Date
    Nov 2007
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView Render() Issue

    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.

  6. #6
    Join Date
    Nov 2007
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView Render() Issue

    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.

    Qt Code:
    1. qreal lwidth = 1/(1000*option->levelOfDetail)*width; // required to multiple by 1000 to set correctly for the scene
    2. QPen myPen = pen();
    3. myPen.setWidth((int)lwidth);
    4. painter->setPen( myPen );
    5. setLine( get_line(widget,line()) );
    6. painter->drawLine( line() );
    To copy to clipboard, switch view to plain text mode 

    I also found that if I were to call QGraphicLineItem:aint() 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.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView Render() Issue

    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.

  8. #8
    Join Date
    Nov 2007
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView Render() Issue

    The Plot when printed in Linux


    The Plot when printed in Windows


    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.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView Render() Issue

    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.

  10. #10
    Join Date
    Nov 2007
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView Render() Issue

    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.

  11. #11
    Join Date
    Nov 2007
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView Render() Issue

    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 ).

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView Render() Issue

    Quote Originally Posted by paul567 View Post
    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.

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView Render() Issue

    What happens if you call QGraphicsScene::render insted of QGraphicsView::render?

Similar Threads

  1. QGraphicsView rendering issue
    By guilugi in forum Qt Programming
    Replies: 9
    Last Post: 6th April 2007, 09:09
  2. How to use QGraphicsView and Scene right ...
    By Mike in forum Qt Programming
    Replies: 6
    Last Post: 22nd January 2007, 08:51

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.