PiedPiper
PiedPiper

Reputation: 5785

How to print a Qt dialog or window?

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.

Upvotes: 4

Views: 7239

Answers (2)

Ariya Hidayat
Ariya Hidayat

Reputation: 12561

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.

Upvotes: 8

corné
corné

Reputation: 798

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();

Upvotes: 8

Related Questions