Results 1 to 2 of 2

Thread: Painter not active when exporting to pdf with different filename

  1. #1
    Join Date
    Jan 2008
    Location
    Brasil
    Posts
    131
    Thanks
    18
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Painter not active when exporting to pdf with different filename

    I need to export multiple reports to pdf, but when I use the function QPrinter::setOutputFileName the error occurs:
    QPainter::begin(): Returned false
    QPainter::translate: Painter not active
    QPainter::setPen: Painter not active
    QPainter::setRenderHint: Painter must be active to set rendering hints

    See the code:

    Qt Code:
    1. for (int i = 0; model->rowCount() > i; i++)
    2. {
    3. int x = model->data(model->index(i, 0), Qt::DisplayRole).toInt();
    4.  
    5. QString filename = dir.append(QDir::separator()).append(model->data(model->index(i, 1), Qt::DisplayRole).toString()).append(".pdf");
    6.  
    7. QPrinter * printer = new QPrinter(QPrinter::HighResolution);
    8. printer->setOutputFormat(QPrinter::PdfFormat);
    9. printer->setOrientation(QPrinter::Portrait);
    10. printer->setPaperSize(QSizeF(210, 297), QPrinter::Millimeter);
    11. printer->setFullPage(true);
    12. printer->setOutputFileName(filename);
    13.  
    14. QPrintPreviewWidget * printPreview = new QPrintPreviewWidget(printer, this);
    15.  
    16. Report * r = new Report(x, this); // Report generation class
    17. connect(printPreview, SIGNAL(paintRequested(QPrinter*)), r, SLOT(Print(QPrinter*)));
    18.  
    19. printPreview->print();
    20. disconnect(printPreview, SIGNAL(paintRequested(QPrinter*)), r, SLOT(Print(QPrinter*)));
    21. }
    To copy to clipboard, switch view to plain text mode 

    If you remove the function "printer->setOutputFileName, will work, but will send directly to the printer. How can I solve this?

    Thanks,

    Marcelo E. Geyer

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Painter not active when exporting to pdf with different filename

    Well, you could start by making sure that file name is what you think it is, contains no illegal characters for a file name, and that the file can be written in the location specified.

    Also these points are worth thinking about:
    • You need not be using QDir::separator() for the reasons described in the documentation.
    • The QPrinter need not be allocated on the heap; it only makes a memory leak at the moment.
    • The QPrintPreviewWidget is totally unnecessary: just call the Print() function directly.
    • IMHO "QSizeF(210, 297), QPrinter::Millimeter" is more obvious when expressed as QPrinter::A4.

Similar Threads

  1. painter not active
    By athulms in forum Qt Programming
    Replies: 1
    Last Post: 17th August 2011, 17:02
  2. Replies: 4
    Last Post: 12th August 2011, 12:20
  3. Painter not active
    By bainedyal in forum Qt Programming
    Replies: 3
    Last Post: 4th August 2011, 10:06
  4. Exporting to RTF
    By gruszczy in forum Qt Programming
    Replies: 2
    Last Post: 19th February 2009, 00:00
  5. Painter not active!
    By Caius Aérobus in forum Qt Programming
    Replies: 7
    Last Post: 30th March 2006, 15:44

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.