Important: Please read the Qt Code of Conduct -

How to divide a QGraphicsScene on multiple pdf pages

  • Hello everyone,

    I'm facing big difficulties trying to get the content of my QGraphicsScene into a pdf made of multiple pages. I somehow manage to get a pdf file with its 2 pages different from one another, but I am not able to place custom QGraphicsItem at the place I want them to be.

    Here's an example : my scene (custom class derived from QGraphicsScene) has the size of two A4 landscape pages and I would like to render it in a 2 pages pdf file like that : the first half of the scene to the first page and the other half to the second. Here's what I do :
    @myScene = new Scene();
    myScene->setSceneRect(0.0, 0.0, 297.0, 210.0 * 2.0);
    //populate myScene with custom class derived from QGraphicsItem
    myView = new View(myScene);

    //to export to pdf
    QPrinter myPrinter(QPrinter::HighResolution);
    QPainter myPainter(&myPrinter);

    //print first page
    QRect view = QRect(0.0, 0.0, 297.0, 420.0);
    myView->render(&myPainter, view, view);

    //print second page
    QRect view2 =QRect(0.0, 421.0, 297.0, 420.0);
    myView->render(&myPainter, view, view);@

    Each object of my custom item has a name and an order for placing them on the scene (setPos(10, order * PROCESS_HEIGHT);). In the paint method, they draw their name :
    @QRectF textRect(10, 10, 200, 10);
    QString message(QString::number(order) + " process " + name);
    painter->drawText(textRect, message);@

    The pdf created shows a list of name but they are displayed at the middle of the page although their position is set to (10, 10), (10, 20), ... And the string is not complete but I checked the boundingRect() and it is larger enough.

    Several problems that I don't get :

    1. Why do I must choose 50 dpi on line 8 ? It seems very low but bigger values output very little texts.
    2. Why do I have to set the height of the area of the scene visualized by myView to 420 and not 210 as this should be half the scene's height ?
    3. What must I do to get the result wanted ? 2 pages, each one with a different half of the scene and with entire names starting from the left (I tried changing the x position to -150 but the first lines on second page are still on the middle...)

    Obviously I don't get something, but what ?! Thanks for the reading and helping

Log in to reply