Results 1 to 3 of 3

Thread: Qt4 : QPainter::setRedirected doesn't work

  1. #1
    Join Date
    Oct 2006
    Location
    Bangalore
    Posts
    32
    Qt products
    Qt4
    Platforms
    Windows

    Exclamation 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.

    Qt Code:
    1. void print()
    2. {
    3. QPrinter printer;
    4. printer.setOutputFormat(QPrinter::PdfFormat);
    5. printer.setOutputFileName("C:/sample.pdf");
    6. printer.setPageSize(QPrinter::A4);
    7.  
    8. QPainter::setRedirected(widget, &printer);
    9. QPaintEvent pe(this->rect());
    10. qApp->sendEvent(this, &pe);
    11. QPainter::restoreRedirected(widget);
    12. widget->repaint();
    13. }
    To copy to clipboard, switch view to plain text mode 

    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
    Last edited by marcel; 20th June 2008 at 18:56. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt4 : QPainter::setRedirected doesn't work

    From Qt Documentation

    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
    Qt Code:
    1. void
    2. MyWidget::print()
    3. {
    4. QPrinter printer;
    5.  
    6. ....
    7.  
    8. QPixmap myPix = QPixmap::grabWidget(this);
    9.  
    10. QPainter painter;
    11. painter.begin(&printer);
    12.  
    13. painter->drawPixmap(0, 0, myPix);
    14.  
    15. painter.end();
    16. }
    To copy to clipboard, switch view to plain text mode 

    QPixmap::grabWidget calls QWidget::paintEvent()
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Oct 2006
    Location
    Bangalore
    Posts
    32
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt4 : QPainter::setRedirected doesn't work

    Hi,
    I did this, but it doesn't create sample.pdf as desired.

    Qt Code:
    1. QPrinter printer;
    2. printer.setOutputFormat(QPrinter::PdfFormat);
    3. printer.setOutputFileName("C:/sample.pdf");
    4. printer.setPageSize(QPrinter::A4);
    5.  
    6. QPixmap myPix = QPixmap::grabWidget(this);
    7. QPainter painter;
    8. painter.begin(&printer);
    9. painter.drawPixmap(0, 0, myPix);
    10. painter.end();
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Copy / Paste doesn't work with LineEdit
    By ia32 in forum Qt Tools
    Replies: 2
    Last Post: 5th May 2008, 21:44
  2. QActions don't work with menubar hidden
    By Pepe in forum Qt Programming
    Replies: 1
    Last Post: 16th August 2007, 01:04
  3. Change work area OS
    By pakulo in forum Qt Programming
    Replies: 15
    Last Post: 15th May 2007, 07:20
  4. QTextEdit Justify align making work
    By dec0ding in forum Qt Programming
    Replies: 2
    Last Post: 13th January 2006, 12:02

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.