printing of dockWidget contents
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.
Code:
int w = printer.width();
int h = printer.height();
dockWidget_
->render
(&p,
QPoint(), region
);
Re: printing of dockWidget contents
Scale the painter of the printer using QPainter::setWindow() and QPainter::setViewport().
Re: printing of dockWidget contents
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...
Re: printing of dockWidget contents
Quote:
Originally Posted by
user
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?
Re: printing of dockWidget contents
The code I had was:
Code:
if ( dialog.
exec() == QDialog::Accepted ) {
int w = printer.width();
int h = printer.height();
dockWidget_
->render
(&p,
QPoint(), region
);
}
I thought maybe returning from the dialog the printer already had the page size defined.
Re: printing of dockWidget contents
It does have a page size defined, but it doesn't mean your widget will be scaled to fit the page.
Re: printing of dockWidget contents
Use QwtPlot::print() instead of QWidget::render().
Uwe
Re: printing of dockWidget contents
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).