Results 1 to 13 of 13

Thread: problem printing a multi page report

  1. #1
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question problem printing a multi page report

    I can't wrap my mind around printing multiple pages using QPrintPreviewDialog. I searched the forum and the web, nothing helpful came up.

    My goal is to print invoices, each page consists of a letter head at the top, followed by the details for the particular customer.

    I have no trouble doing this for a single page but I wanted to get a preview with N invoices, each on a separate page.

    Here is what I do now:

    Qt Code:
    1. void MainWindow::prepHeader(QTextEdit &t)
    2. {
    3.  
    4. t.append(QString("a nice letter head"));
    5. t.append("\n");
    6.  
    7. t.append(QString("customer info\n"));
    8.  
    9. t.append("\n\n\n\n\n\n______________________________________________________________\n"); //divider and some spacing
    10.  
    11. }
    12.  
    13.  
    14. void MainWindow::printReport(QTextEdit &pte, QString detail, bool bPreview)
    15. {
    16.  
    17. QPrintPreviewDialog pvw(printer,this);
    18.  
    19. if (bPreview)
    20. connect(&pvw, SIGNAL(paintRequested(QPrinter *)), SLOT(printPreview(QPrinter *)));
    21.  
    22. prepHeader(pte); //show a company specific letter head
    23.  
    24. pte.append(detail);
    25.  
    26.  
    27. if (bPreview)
    28. {
    29. pvw.exec();
    30. pvw.disconnect();
    31. pte.clear();
    32. }
    33. else
    34. pte.print(printer);
    35.  
    36. }
    37.  
    38. void MainWindow::printPreview(QPrinter *printer)
    39. {
    40. rpt.print(printer);
    41.  
    42. }
    43.  
    44. void MainWindow::printInvoice()
    45. {
    46. QString detail;
    47. QTextStream strm(&detail);
    48.  
    49.  
    50. strm << "customer details" << endl;
    51.  
    52. printReport(rpt, detail, true);
    53.  
    54. detail.clear();
    55. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: problem printing a multi page report

    Hi,

    What is the rpt variable that you use ? Your variable names are rather cryptic.

    Typically, you write a method that prints your entire report, be it either 1 page or multiple pages. For starting a new page, just use QPrinter::newPage(). Something like this :

    Qt Code:
    1. void CMainForm::paintMyReport( QPrinter * printer )
    2. {
    3. QPainter painter;
    4. painter.begin(printer);
    5.  
    6. for( int nPage=0; nPage<m_nLastPage; nPage++ )
    7. {
    8. // print your page
    9.  
    10. if( nPage<m_nLastPage-1 )
    11. printer->newPage();
    12. }
    13. painter.end();
    14. }
    To copy to clipboard, switch view to plain text mode 

    If you want to print it with a preview, do this :
    Qt Code:
    1. QPrintPreviewDialog dlgPrint;
    2. connect( &dlgPrint, SIGNAL(paintRequested(QPrinter*)), SLOT(paintMyReport(QPrinter*)) );
    3. dlgPrint.exec();
    To copy to clipboard, switch view to plain text mode 

    If you want to print it directly, do this :
    Qt Code:
    1. QPrinter printer;
    2. printer.setPrinterName( "CutePDF Writer" );
    3. paintMyReport( &printer );
    To copy to clipboard, switch view to plain text mode 

    Regards,
    Marc

  3. #3
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: problem printing a multi page report

    Thanks Marc,

    I got better results with your suggestions. Inside the pages loop, am I limited to using painter.drawText() or can I append text to a QTextEdit and then call
    Qt Code:
    1. QTextEdit::print()
    To copy to clipboard, switch view to plain text mode 
    ?

  4. #4
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: problem printing a multi page report

    Consider this code:
    Qt Code:
    1. void MainWindow::print(QPrinter *printer)
    2. {
    3. QTextEdit textEdit[2];
    4.  
    5. textEdit[0].append("First Page");
    6. textEdit[1].append("Second page");
    7.  
    8.  
    9. textEdit[0].print(printer);
    10. printer->newPage();
    11. textEdit[1].print(printer);
    12.  
    13. }
    14.  
    15. void MainWindow::on_pushButton_clicked()
    16. {
    17.  
    18. QPrinter printer(QPrinter::HighResolution);
    19. QPrintPreviewDialog preview(&printer,this);
    20. connect(&preview, SIGNAL(paintRequested(QPrinter*)),SLOT(print(QPrinter *)));
    21. preview.exec();
    22.  
    23.  
    24. }
    To copy to clipboard, switch view to plain text mode 

    I get a single page print preview showing 'second page' and a page number '1' at the bottom.

    where did the first page go?
    Last edited by schnitzel; 17th January 2011 at 19:02.

  5. #5
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: problem printing a multi page report

    Hi,
    Afaik, printing must always be done with a QPainter object.

    See Printing with Qt, and then the section 'printing complex widgets'. It says how to print a QTextEdit and so.

    Maybe if you do a QPainter.begin() as in my example, your first page won't get lost. I'm not sure.

    Best regards,
    Marc

  6. #6
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: problem printing a multi page report

    Quote Originally Posted by marcvanriet View Post
    Hi,
    Afaik, printing must always be done with a QPainter object.

    See Printing with Qt, and then the section 'printing complex widgets'. It says how to print a QTextEdit and so.

    Maybe if you do a QPainter.begin() as in my example, your first page won't get lost. I'm not sure.

    Best regards,
    Marc
    QTextEdit seems to be an exception as it uses a QPrinter and the :: print method (see the table at the bottom of the page in your link).

    I will play around a bit more later and post some more examples of what I've tried so far.

  7. #7
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: problem printing a multi page report

    If I try the following:

    Qt Code:
    1. void MainWindow::print(QPrinter *printer)
    2. {
    3. QTextEdit textEdit[2];
    4.  
    5. textEdit[0].append("\t\tFirst Page");
    6. textEdit[1].append("\t\tSecond page");
    7.  
    8. QPainter painter;
    9. painter.begin(printer);
    10.  
    11. textEdit[0].document()->drawContents(&painter, printer->pageRect());
    12. printer->newPage();
    13. textEdit[1].document()->drawContents(&painter, printer->pageRect());
    14.  
    15. painter.end();
    16.  
    17. }
    18.  
    19. void MainWindow::on_pushButton_clicked()
    20. {
    21.  
    22. QPrinter printer(QPrinter::HighResolution);
    23. QPrintPreviewDialog preview(&printer,this);
    24. connect(&preview, SIGNAL(paintRequested(QPrinter*)),SLOT(print(QPrinter *)));
    25. preview.exec();
    26. }
    To copy to clipboard, switch view to plain text mode 

    I get two blank pages... what am I doing wrong?

  8. #8
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: problem printing a multi page report

    Hi,

    I get the same results. I think it has something to do with the printer resolution. In the code you just posted, put the following 2 lines in on_pushButton_clicked() after creating the printer object :
    Qt Code:
    1. printer.setFullPage(true);
    2. printer.setResolution( 90 );
    To copy to clipboard, switch view to plain text mode 

    You will see some text appearing now.

    I don't know how it should be done properly with QTextEdit. I have always used QPainters drawText up to now.

    Regards,
    Marc

  9. #9
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: problem printing a multi page report

    Quote Originally Posted by marcvanriet View Post
    Hi,

    I get the same results. I think it has something to do with the printer resolution. In the code you just posted, put the following 2 lines in on_pushButton_clicked() after creating the printer object :
    Qt Code:
    1. printer.setFullPage(true);
    2. printer.setResolution( 90 );
    To copy to clipboard, switch view to plain text mode 

    You will see some text appearing now.

    I don't know how it should be done properly with QTextEdit. I have always used QPainters drawText up to now.

    Regards,
    Marc
    Hmmm, still not working. Here is my current code:

    Qt Code:
    1. void MainWindow::print(QPrinter *printer)
    2. {
    3. QTextEdit textEdit[2];
    4.  
    5. textEdit[0].append("\t\tFirst Page");
    6. textEdit[1].append("\t\tSecond page");
    7.  
    8. QPainter painter;
    9. painter.begin(printer);
    10.  
    11. textEdit[0].document()->drawContents(&painter, printer->pageRect());
    12. printer->newPage();
    13. textEdit[1].document()->drawContents(&painter, printer->pageRect());
    14.  
    15. painter.end();
    16.  
    17. }
    18.  
    19. void MainWindow::on_pushButton_clicked()
    20. {
    21.  
    22. QPrinter printer(QPrinter::HighResolution);
    23. printer.setFullPage(true);
    24. printer.setResolution(90);
    25.  
    26. QPrintPreviewDialog preview(&printer,this);
    27. connect(&preview, SIGNAL(paintRequested(QPrinter*)),SLOT(print(QPrinter *)));
    28. preview.exec();
    29. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: problem printing a multi page report

    Found it (more or less).

    Tried exactly your code, and appearently it doesn't work if your QTextEdit is just some object you create.
    I did my tests using textedits that I had put on my dialog, and then it works.

    Regards,
    Marc

  11. The following user says thank you to marcvanriet for this useful post:

    schnitzel (19th January 2011)

  12. #11
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: problem printing a multi page report

    Quote Originally Posted by marcvanriet View Post
    Found it (more or less).

    Tried exactly your code, and appearently it doesn't work if your QTextEdit is just some object you create.
    I did my tests using textedits that I had put on my dialog, and then it works.

    Regards,
    Marc
    You're my hero...

    I'm not quite sure yet how to deal with adding the QTextEdit - I can't just simply make it invisible on my window (already tried that). Making it invisible in the 'on_pushButton_clicked()' seems to work fine though.

    Anyway, thanks so much for your help. This problem has been nagging me for a couple of days now.

  13. #12
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: problem printing a multi page report

    Ok, got a little further. The printpreview now works, but it isn't behaving the way I want. Now that I have added the QTextEdit to the main window, the text gets formatted according to the widget's dimension. I guess that is kind of expected. What I really wanted is to create a nice looking multi page report and my reason for using QTextEdit is the Richly formatted text. What I didn't anticipate is the way the text is depending on the QTextEdit widget's dimension. Is there no way for me to create nicely formatted text but defer line wrapping etc. until it gets drawn onto the printer/painter device?
    Would I have to print to pdf in order to get what I want? I guess I could just try it, but I was wondering if I'm approaching this from the right direction.

    Here is my latest code.
    Qt Code:
    1. //Mainwindow constructor contains: printer = new QPrinter(QPrinter::ScreenResolution);
    2.  
    3. void MainWindow::print(QPrinter *printer)
    4. {
    5.  
    6. QPainter painter;
    7. painter.begin(printer);
    8.  
    9. ui->textEdit->append("first page this is a fairly long line to see what happens when it is being wrapped");
    10. ui->textEdit->document()->drawContents(&painter, printer->pageRect());
    11.  
    12. printer->newPage();
    13. ui->textEdit->clear();
    14. ui->textEdit->append("second page");
    15. ui->textEdit->document()->drawContents(&painter, printer->pageRect());
    16. ui->textEdit->clear();
    17.  
    18. painter.end();
    19. }
    20.  
    21. void MainWindow::on_pushButton_clicked()
    22. {
    23.  
    24. QPrintPreviewDialog preview(printer,this);
    25. connect(&preview, SIGNAL(paintRequested(QPrinter*)),SLOT(print(QPrinter *)));
    26. preview.exec();
    27.  
    28. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by schnitzel; 23rd January 2011 at 01:50.

  14. #13
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: problem printing a multi page report

    Ok, I figured out how to approach it.
    I'm abandoning the entire appending to QTextEdit in favor of creating a QTextDocument and using HTML. Here is the code, in the hope that it will be helpful to somebody.

    Qt Code:
    1. void MainWindow::print(QPrinter *printer)
    2. {
    3.  
    4. QPainter painter;
    5. painter.begin(printer);
    6.  
    7. doc.setHtml( "<p>A QTextDocument can be used to present formatted text "
    8. "in a nice way.</p>"
    9. "<p align=center>It can be <b>formatted</b> "
    10. "<font size=+2>in</font> <i>different</i> ways.</p>"
    11. "<p>The text can be really long and contain many "
    12. "trying a really long line to see how it wraps "
    13. "more text to see how long I can make this line show "
    14. "paragraphs. It is properly wrapped and such...</p>" );
    15.  
    16. QRect rect = printer->pageRect();
    17. doc.setTextWidth(rect.width());
    18. doc.drawContents(&painter);
    19. printer->newPage();
    20.  
    21.  
    22. doc.drawContents(&painter);
    23.  
    24. painter.end();
    25. }
    To copy to clipboard, switch view to plain text mode 

    Feel free to elaborate if there is a better way to achieve this.

Similar Threads

  1. multi page Application
    By ilpaso in forum Qt Programming
    Replies: 2
    Last Post: 3rd September 2010, 10:36
  2. Printing to a plain text (txt) file from a report
    By luizofoca in forum Qt Programming
    Replies: 0
    Last Post: 10th June 2010, 20:02
  3. Printing custom report with QWT
    By maluedo in forum Qwt
    Replies: 2
    Last Post: 6th April 2010, 12:09
  4. Replies: 1
    Last Post: 10th February 2009, 07:21
  5. Multi-Page PDF Output
    By igor in forum Qt Programming
    Replies: 1
    Last Post: 9th January 2007, 05:10

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.