Difficulty with Painting to QGraphicsScene and PDF



  • I am trying to do a painting made up of lots of small QPolygons.
    My process is to use a QGraphicsScene, add Polygons through a loop, take the itemsBoundingRect and create a QImage, and then putting this on a PDF. Here is my code:

    //Set up the painter for the PDF document
    QPrinter printer(QPrinter::HighResolution);
    QPdfWriter pdf(workingDirectory + "/Chart.pdf");
    QPainter painter(&pdf);
    QGraphicsScene chartPainter;
    
    //add each polygon through a loop
    for (int m = 0; m < polygonNumber.size(); m = m + 1)
    {
        // some code here to find the x and y coordinates for the corners of the polygon
        //..........x: horiz1 horiz2 horiz3 horiz4 y:vert1 vert2 vert3 vert4
        // some code here to find the x and y coordinates for the corners of the polygon
                        
        QPolygonF element;
        element << QPointF(horiz1, vert1)
                << QPointF(horiz2, vert2)
                << QPointF(horiz3, vert3)
                << QPointF(horiz4, vert4);
    
        //set pen width
        QPen plotPen;
        plotPen.setWidth(0);
    
        //set brush color
        QBrush plotBrush;
        plotBrush.setColor("some code to determine the color depending on which polygon is being drawn");
    
        //add polygon to QGraphicsScene chartPainter with pen and brush
        chartPainter.addPolygon(element,plotPen,plotBrush);
    
    }   //end loop for adding each polygon
    
    //take the itemsBoundingRect size as a QImage - consider which QImage Format is needed
    QImage paintedChart(chartPainter.itemsBoundingRect().size().toSize(), QImage::Format_RGBA8888);
    
    //set location on pdf page and 'paint' it there
    int h = painter.window().height() * 0.5;
    int w = h * 1.3;
    int x = (painter.window().width()  / 2) - (w/2);
    int y = (painter.window().height() / 2) - (h/2) - 1000;
    
    //paint the paintedChart using the painter(pdf)
    painter.drawImage(QRectF(x,y,w,h), paintedChart, QRectF(0,0,paintedChart.width(), paintedChart.height()));
    

    I have tried some variations using QPixmap, but I never know the size of the image I need, hence using QGraphicsScene.

    The code gives me just a black box or a hazy mess depending on which Image format I use. Anyone have any ideas?



  • Anyone able to help with this? Still cant seem to get it to work



  • @ahmedalshawi

    Have a look at this:

    QImage image(fn);
    QPainter painter(&image);
    painter.setRenderHint(QPainter::Antialiasing);
    scene.render(&painter);
    image.save("file_name.png") // Try if PDF works here too
    
    

    From:
    https://stackoverflow.com/questions/7451183/how-to-create-image-file-from-qgraphicsscene-qgraphicsview



  • @pl45m4 said in Difficulty with Painting to QGraphicsScene and PDF:

    @ahmedalshawi

    Have a look at this:

    QImage image(fn);
    QPainter painter(&image);
    painter.setRenderHint(QPainter::Antialiasing);
    scene.render(&painter);
    image.save("file_name.png") // Try if PDF works here too
    
    

    From:
    https://stackoverflow.com/questions/7451183/how-to-create-image-file-from-qgraphicsscene-qgraphicsview

    I previously found this link, I get nothing but a black hazy mess.


  • Lifetime Qt Champion

    Hi
    Can you try the sample
    https://doc.qt.io/qt-5/qtwidgets-graphicsview-diagramscene-example.html
    (its available directly in Creator)
    and alter its About function ( mainwindow.cpp 322 )

    void MainWindow::about()
    {
    
        QPixmap image(view->size());
        QPainter painter(&image);
        painter.setRenderHint(QPainter::Antialiasing);
        view->render(&painter);
        image.save("e:/file_name.png");
    
        QMessageBox::about(this, tr("About Diagram Scene"),
                           tr("The <b>Diagram Scene</b> example shows "
                              "use of the graphics framework."));
    }
    

    That gives me.
    Top is the saved result and below is the app.
    alt text



  • @mrjj Thanks for the help. It is getting there but this is how it looks for me:

    0_1566030415105_file_name.png

    Still get a black background with a hazy mess. Any ideas?


  • Lifetime Qt Champion

    @ahmedalshawi
    Ok so that is with same code and same example?
    I wonder if its some sort of driver issue since it should work the same
    otherwise.
    What gfx hardware are you using ?



  • @mrjj
    You are saving as png image. @AhmedAlshawi needs pdf.
    Does png work for you or is the result the same as pdf, so you can see, if it's a general error or just a pdf thing



  • @mrjj
    Exact same example and exact same code - I literally copied and pasted it from your post. I have an i5 7440HQ with Intel HD 620 GPU.

    @Pl45m4
    I ran the example saving as PNG - same as @mrjj. Ultimately though yes I do want this to work to a PDF however if PNGs work, that is fine because I can just write up some code to drop the PNGs into the PDF.


  • Lifetime Qt Champion

    Hi
    Could you try to see if a newer driver is available for the
    Intel HD 620 GPU.

    @Pl45m4 , yes i know he wants pdf :), but i wanted to see if the same sample
    that works on the 3 pc (win 8,10, linux) i tested on - would fail for him, giving a hint about where to look.



  • @mrjj

    Got the latest. No further updates for it. Bizzare that the same code gives different results!


  • Lifetime Qt Champion

    @ahmedalshawi
    Yes its pretty odd. I assume you literally copied the about function to the project so
    its even run the same way. (via the about menu) and not say in constructor or similar.

    Also, since it renders fine to the screen, its very odd
    rendering to an image gives artifacts.
    It looks like the image is slightly corrupted and im really out of ideas why
    it happens.
    Do you have the possibility to try the code on other pc. just to know
    if you can reproduce it elsewhere or its only on that pc.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.