Results 1 to 11 of 11

Thread: [SOLVED] Print to pdf

  1. #1
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default [SOLVED] Print to pdf

    I have made som drawings in a mainwindow and now I would like to export it to a pdf file or postscript. How can I do that?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Print to pdf

    Try painting on a QPrinter (whose output format is set to QPrinter::PdfFormat).

    EDIT: an example:
    Qt Code:
    1. QPrinter printer(QPrinter::HighResolution);
    2. printer.setOutputFormat(QPrinter::PdfFormat);
    3. printer.setOutputFileName("test.pdf");
    4. QPainter painter;
    5. painter.begin(&printer);
    6. // draw the content
    7. // painter.draw..(..);
    8. painter.end();
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 7th April 2006 at 09:31.
    J-P Nurmi

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Print to pdf

    ..and if I want print into a pdf the content of myGLWidget(it draw with paintGL())?
    Regards

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Print to pdf

    Save it into QImage or QPixmap using appropriate methods from QGLWidget and then print as usual.

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Print to pdf

    thanks, to print directly my scene do I need insert an istruction like :
    Qt Code:
    1. QImage qi=qp.convertToImage();
    2. qi.save(base_file + ".png","PNG");
    3. painter.begin(&printer);
    4. /////painter.draw(qi);??????????????? Is it there?
    5. painter.end();
    To copy to clipboard, switch view to plain text mode 
    because my printer starts but print nothing...
    Regards

  6. #6
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,312
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Print to pdf

    Quote Originally Posted by wysota
    Save it into QImage or QPixmap using appropriate methods from QGLWidget and then print as usual.
    PDF is a scalable format, what gets lost doing it via a pixmap/image.
    If vector data has to be painted, one should always try to render the PDF documents with QPainter using drawing primitives like lines, rects ... . Of course the geometry of these primitives has to be in qreals.

    HTH,
    Uwe

  7. #7
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Print - what am I missing

    sorry but my question isn't relative to pdf format; I save the content of myGLWidget in a QImage qi; now I'd like to print it! the printer starts but the sheet is white. What am I missing? Thanks
    Regards

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Print to pdf

    Do you paint anything? See QPainter::drawImage().
    For example:
    Qt Code:
    1. painter.drawImage(0,0,qi);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  9. #9
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Print to pdf

    I tried this but print nothing again....
    Qt Code:
    1. qi.save(base_file + ".png","PNG");
    2. QPrinter printer(QPrinter::HighResolution);
    3. printer.setFullPage( TRUE );
    4. printer.setPageSize(QPrinter::A4);
    5. if ( printer.setup(this) ) { // printer dialog
    6. statusBar()->message( "Printing..." );
    7. QPainter painter;
    8. painter.drawImage(0,0,qi, 10,10, 20,20);
    9. if( !painter.begin( &printer ) ) { // paint on printer
    10. statusBar()->message( "Printing aborted", 2000 );
    11. return;
    12. }
    13.  
    14. painter.end();
    15. statusBar()->message("");
    16. }
    To copy to clipboard, switch view to plain text mode 
    Regards

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Print to pdf

    Quote Originally Posted by Uwe
    PDF is a scalable format, what gets lost doing it via a pixmap/image.
    If vector data has to be painted, one should always try to render the PDF documents with QPainter using drawing primitives like lines, rects ... . Of course the geometry of these primitives has to be in qreals.

    HTH,
    Uwe
    The point is this is an OpenGL widget and I doubt you'll be able to use QPainter directly with it (meaning without GL context). IMHO you have to convert it to a bitmap first.

  11. #11
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Thumbs up Re: Print QGLWidget (SOLVED)

    Qt Code:
    1. if (printer.setup(this)) {
    2. QPainter painter(&printer);
    3. QRect rect = painter.viewport();
    4. QSize size = qi.size();
    5. size.scale(rect.size(), QSize::ScaleMin);
    6. painter.setViewport(rect.x(), rect.y(),
    7. size.width(), size.height());
    8. painter.setWindow(qi.rect());
    9. painter.drawImage(0, 0, qi);
    10. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by mickey; 8th April 2006 at 10:25.
    Regards

Similar Threads

  1. Trouble to print with QPrintPreviewWidget
    By estanisgeyer in forum Qt Programming
    Replies: 5
    Last Post: 24th December 2009, 06:14
  2. QSqlQuery and seek() - doesn't work on first call.
    By amicitas in forum Qt Programming
    Replies: 1
    Last Post: 2nd October 2008, 17:25
  3. Change margin on QTextEdit print()
    By Dii in forum Qt Programming
    Replies: 2
    Last Post: 20th May 2008, 22:45
  4. print QTableWidget
    By chak_med in forum Qt Programming
    Replies: 3
    Last Post: 4th November 2006, 18:46
  5. [QT4] QThread and printing a QList<QPixmap>
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 21: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.