views:

57

answers:

1

How can I print the content of a QGraphicsView in Qt?

Thanks a lot.

+3  A: 

Take a look at the official Qt documentation: http://doc.qt.nokia.com/4.6/graphicsview.html#printing

For further reference:

"Graphics View provides single-line printing through its rendering functions, QGraphicsScene::render() and QGraphicsView::render(). The functions provide the same API: You can have the scene or the view render all or parts of their contents into any paint device by passing a QPainter to either of the rendering functions. This example shows how to print the whole scene into a full page, using QPrinter."

Example:

QGraphicsScene scene;
scene.addRect(QRectF(0, 0, 100, 200), QPen(Qt::black), QBrush(Qt::green));

QPrinter printer;
if (QPrintDialog(&printer).exec() == QDialog::Accepted) {
    QPainter painter(&printer);
    painter.setRenderHint(QPainter::Antialiasing);
    scene.render(&painter);
}
Greg S
Thanks a lot I try it and it run. But I have a problem. My scene rect about (0,0,2700,800) and its unreadable. Do you think any solution about it?
ayla