简体   繁体   中英

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.

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

While you can use grabWidget to get the pixmap representation of the dialog, essentially you will be printing the pixels of the pixmap, ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM