1

So I'm trying to print a window from my application, and use the following code:

QPrinter *printer = new QPrinter;

printer->setResolution(1200);

QPrintDialog *printDialog = new QPrintDialog(printer, this);

if (printDialog->exec() == QDialog::Accepted) {

    QPainter p(printer);

    QPixmap pm = QPixmap::grabWidget(this);

    p.drawPixmap(0,0,pm);
}

To test it I export the image to PDF, but what I get is very low resolution file. Any idea on how to make a high resolution zoomable image? Cause I am printing a graph which should be readable, as it has some small size values on it.

gEdringer
  • 16,284
  • 3
  • 14
  • 27
  • You're grabbing a fixed-resolution pixmap off the widget. What did you expect? You're seeing the correct behavior. You need to render the graph directly to the printer, without using the roundabout pixmap. Ideally, you need to factor out the graph drawing code, and use it directly from the widget and from the printing code. – Kuba hasn't forgotten Monica Apr 13 '15 at 18:42

1 Answers1

5

QPrinter is a subclass of QPaintDevice, thus you may try to use the following QWidget function which will draw directly at it:

void QWidget::​render(QPaintDevice * target,...)

Also there's a QPainter version of the same function if you'd like to create QPainter by yourself.

Predelnik
  • 5,066
  • 2
  • 24
  • 36
  • Could you give me a quick example of use? – gEdringer Apr 13 '15 at 11:43
  • It is also possible to apply scaling factor to the painter, e.g. http://stackoverflow.com/questions/6650219/zooming-function-on-a-qwidget – Archie Apr 13 '15 at 11:44
  • @gEdringer basically just change everything inside `if` in your example to `render (printer);`, maybe then tune rest of the arguments if needed. – Predelnik Apr 13 '15 at 11:46
  • @Predelnik I'm still getting low res image :( I simply want a zoomed in high res picture, but all I get is small size (like 1/5th of the picture) window with pixelized resolution which is very bad :/ – gEdringer Apr 13 '15 at 11:58
  • @gEdringer Well then you need to think about how you're gonna scale your widget I guess: resizing widget itself and scaling `QPainter` will give you different results. At first try as @Archie suggested to create `QPainter` and use its function `scale` and then use render on that `QPainter`.Also if you print for example in pdf using the method I suggested at least fonts and the rest of scalable stuff will be probably infinitely zoomable (so it's not technically pixelized). But I also think that widget may contain a lot of constant size pixel objects used for its drawing, they may not scale well – Predelnik Apr 13 '15 at 12:03