Qt4 : QPainter::setRedirected doesn't work
Hi,
I am trying to divert the paint operations of a widget to the printer.
But it doesn't seem to work for me.
following is the code.
Code:
void print()
{
printer.
setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName("C:/sample.pdf");
QPainter::setRedirected(widget,
&printer
);
qApp->sendEvent(this, &pe);
widget->repaint();
}
Initially, i used widget->repaint() instead of sendevent(). But didnt work either.
If i query redirected() function on the widget before restoring the redirection , it is correctly showing the redirected device as printer. I wonder still why this code doesn't work!
Am i doing any mistake here?
Pls help
-
Ankitha
Re: Qt4 : QPainter::setRedirected doesn't work
From Qt Documentation
Quote:
void QPainter::setRedirected ( const QPaintDevice * device, QPaintDevice * replacement, const QPoint & offset = QPoint() ) [static]
Redirects all paint commands for the given paint device, to the replacement device. The optional point offset defines an offset within the source device.
The redirection will not be effective until the begin() function has been called; make sure to call end() for the given device's painter (if any) before redirecting. Call restoreRedirected() to restore the previous redirection.
In general, you'll probably find that calling QPixmap::grabWidget() or QPixmap::grabWindow() is an easier solution.
sendEvent "post" an event then the paintEvent method is invoked after you exit from print.
You can do this
Code:
void
MyWidget::print()
{
....
painter.begin(&printer);
painter->drawPixmap(0, 0, myPix);
painter.end();
}
QPixmap::grabWidget calls QWidget::paintEvent()
Re: Qt4 : QPainter::setRedirected doesn't work
Hi,
I did this, but it doesn't create sample.pdf as desired.
Code:
printer.
setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName("C:/sample.pdf");
painter.begin(&printer);
painter.drawPixmap(0, 0, myPix);
painter.end();