How do I get Qt to print a complete dialog or window?
I could dump the window contents with an external program like xwd
and print that, but I would prefer to do it all with Qt.
views:
1252answers:
2
+5
A:
Use QPixmap::grabWidget (or QPixmap::grabWindow for external window). Something like this:
QPixmap pix = QPixmap::grabWidget(myMainWindowWidget);
Dunno if you really mean to print it to a printer, if so:
QPrinter printer(QPrinter::HighResolution);
QPainter painter;
painter.begin(&printer);
painter.drawPixmap (0, 0, &pix);
painter.end();
corné
2009-03-25 11:28:34
pix.grabWidget(myMainWindowWidget) fails for me.I have to use:QPixmap pix = QPixmap::grabWidget(myMainWindowWidget);
PiedPiper
2009-03-25 12:13:28
Ah ok, because it's a static method. I edited my answer.
corné
2009-03-25 12:18:46
QPixmap::grabWidget was good enough for what I needed, but Ariya's solution QWidget::render() is even better
PiedPiper
2009-03-25 21:24:44
+4
A:
While you can use grabWidget to get the pixmap representation of the dialog, essentially you will be printing the pixels of the pixmap, i.e. the dialog is rasterized a the screen resolution and then scaled to the printer resolution. This may or may not result in some artifacts.
Another way to do it is by using QWidget::render() function that takes a paint device. This way, you can pass your printer as the paint device. The dialog is now "drawn" onto the printer with the the printer's resolution.
Ariya Hidayat
2009-03-25 20:30:10