Hello,

I seem to have a small problem here with the render function of the
QGraphicsScene.
I'm trying to print parts of a scene onto several pages, however it
seems that
the render method does the scaling as requested , but does not clip the
origin
to the source QRectF.
The scene contains several items that are all arranged in a tree like
structure, something like this:

    |
+++++++++  <= bar that is parent of all items
| | | | |
O O O O O  <= a set of items below it.


and if I restrain the source QRectF to one or more items it will still print
the parent and the other children outside of the given rectangle. Filling up
the rest of the page which I definitely don't want.

I attached a small minimal example to demonstrate what I mean.
If I do not have the parent-item each ellipse ends up on a separate page
in the
first PDF generated. As soon as a attach them to a parent you will see
in the
second pdf that it will draw all the items to the right also...

Is there a way to avoid this? Or am I obliged to first render onto a
temporary image that I then copy towards the printer(or pdf)?

rgds,

David H

PS: I'm using Qt 4.8.1 on linux and 4.7.4 on windows 7 here, both of
course giving the same result :-)


-- 
openMSX - the open source MSX emulator that aims for perfection
http://openmsx.sf.net/

#include <QtGui/QApplication>
#include <QGraphicsView>
#include <QGraphicsTextItem>
#include <QPrinter>
#include <QDebug>

QGraphicsScene* scene;
void print(QString filename);

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QGraphicsView* view = new QGraphicsView();
    scene = new QGraphicsScene();
    view->setScene(scene);

    //Some crude page numbers
    QGraphicsTextItem* text1 = scene->addText("1");
    QGraphicsTextItem* text2 = scene->addText("2");
    QGraphicsTextItem* text3 = scene->addText("3");
    text1->setScale(3.0);
    text2->setScale(3.0);
    text3->setScale(3.0);
    text1->setPos(0.0 ,0.0);
    text2->setPos(100.0 ,0.0);
    text3->setPos(200.0 ,0.0);
    //The items that we want on a seperate page each
    QGraphicsEllipseItem* el1 = scene->addEllipse(0,100, 100,100);
    QGraphicsEllipseItem* el2 = scene->addEllipse(100,100, 100,100);
    QGraphicsEllipseItem* el3 = scene->addEllipse(200,100, 100,100);
    //Some markers in the ellipse
    scene->addLine(25,150, 75,150);
    scene->addLine(150,125, 150,175);
    scene->addLine(225,125, 275,175);
    view->show();
    // A PDF like we expected
    print("pdf1.pdf");
    //Now we add a parent to connect the tops of the ellipses
    QGraphicsLineItem* line = scene->addLine(50,100,250,100);
    el1->setParentItem(line);
    el2->setParentItem(line);
    el3->setParentItem(line);
    // A "faulty" pdf, the first page has all 3 items
    // the second page display still two items
    // the 3th page is "correct"
    print("pdf2.pdf");

    return app.exec();
}

void print(QString filename)
{
    //Print to a pdf file
    QPrinter* printer = new QPrinter(QPrinter::ScreenResolution);
    printer->setPaperSize(QPrinter::A4);
    printer->setOrientation(QPrinter::Landscape);
    printer->setOutputFormat(QPrinter::PdfFormat);
    printer->setOutputFileName(filename);

    QPainter* painter = new QPainter();
     bool ok = painter->begin(printer);
    Q_ASSERT(ok);

    QRect todrawrect;
    QRect destrect=printer->pageRect();

    for (int i=0;i<3;i++){
        todrawrect.setRect(100*i,0,100,200);
        scene->render(painter,destrect,todrawrect,Qt::KeepAspectRatio);
        if (i<2){printer->newPage();};
    }
    painter->end();
    delete painter;
    delete printer;
}
QT       += core gui

TARGET = printpdf
TEMPLATE = app


SOURCES += main.cpp
_______________________________________________
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest

Reply via email to