PDA

View Full Version : printing of dockWidget contents



user
14th May 2008, 06:35
I use Qt 4.4.0 and I have a dockWidget which includes a label and a QwtPlot (Qwt 5.1.0) in a layout.
I'm trying to print the dockWidget into a .pdf but I have a problem with the size of the printed widget.

The resulting .pdf shows the dockWidget (label and plot) but it doesn't use the whole page. If I change the size of the dockWidget before printing, the printed dockWidget size changes as well (but not taking the full page size).
When the renderFlags are set to (DrawWindowBackground | DrawChildren) The background takes the whole page but the window doesn't.

This is the relevant bit of my code. I'll be happy to consider any other ways of printing my dockWidget.


QPrinter printer;
int w = printer.width();
int h = printer.height();
QRegion region(0,0,w,h);
QPainter p(&printer);
dockWidget_->render(&p, QPoint(), region);

wysota
14th May 2008, 07:13
Scale the painter of the printer using QPainter::setWindow() and QPainter::setViewport().

user
14th May 2008, 23:57
I've added those commands but they didn't seem to make a difference, probably because I use the print dialog before I define the painter. I should have mentioned it but I didn't think it was relevant.
I had a look at the width and height values that are sent to the painter and the width and height of my wiget. The values for the printer didn't change but the values of the widget depend on it's size.
My solution was to resize the widget to the width and height of the printer page just before I print it and then resize it back to it's original size after the print.
It worked and the window doesn't have time to resize on the screen.

I just discovered I will have to print only the plot and not the whole widget, so I guess I'll have to change the code and use something else anyway...

wysota
15th May 2008, 08:43
I've added those commands but they didn't seem to make a difference, probably because I use the print dialog before I define the painter.
How is that possible if you need a painter to be able to print?

user
15th May 2008, 23:05
The code I had was:


QPrinter printer;
QPrintDialog dialog(&printer);
if ( dialog.exec() == QDialog::Accepted )
{
int w = printer.width();
int h = printer.height();
QRegion region(0,0,w,h);
QPainter p(&printer);
dockWidget_->render(&p, QPoint(), region);
}
I thought maybe returning from the dialog the printer already had the page size defined.

wysota
16th May 2008, 08:23
It does have a page size defined, but it doesn't mean your widget will be scaled to fit the page.

Uwe
17th May 2008, 16:19
Use QwtPlot::print() instead of QWidget::render().

Uwe

user
18th May 2008, 23:07
Yes, this is what I'm using now and it works the way I want.
The problem started because I needed to show an extra title with the plot (the plot only has one title and I needed 2) but I solved that by using a marker and locating it always on the top or bottom of the plot (depending on user selection).