Results 1 to 1 of 1

Thread: QWebView: print problems

  1. #1
    Join Date
    Nov 2012
    Posts
    8
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QWebView: print problems

    I'm trying to create report via QWebView, show it via QPrintPreviewDialog and print it. Suppose I want to create the 100-rows table splitted onto several pages and add current row number to the footer of each page (abstract variant of my real task). My code:

    Qt Code:
    1. void MainWindow::preview(){
    2. QPrinter printer;
    3. printer.setPageSize(QPrinter::A4);
    4. printer.setOrientation(QPrinter::Portrait);
    5. printer.setPageMargins(10,10,10,10,QPrinter::Millimeter);
    6.  
    7. QPrintPreviewDialog print_preview(&printer, this);
    8. print_preview.setWindowState(Qt::WindowMaximized);
    9. connect(&print_preview, SIGNAL(paintRequested(QPrinter*)), this, SLOT(paint_pages(QPrinter*)));
    10. print_preview.exec();
    11. }
    12.  
    13. void MainWindow::paint_pages(QPrinter *printer){
    14. QList<QWebView*> pages;
    15. QWebView *current = 0;
    16. QPainter painter(printer);
    17. int i = 0;
    18. while(i <= 100){
    19. current = new QWebView();
    20. pages << current;
    21. i = populate_web(current, printer, i);
    22. }
    23. int pc = pages.count();
    24. for(i = 0; i < pc; i++){
    25. if(i != 0) printer->newPage();
    26. pages.at(i)->render(&painter);
    27. }
    28. for(i = 0; i < pc; i++)
    29. delete pages.at(i);
    30. }
    31.  
    32.  
    33. int MainWindow::populate_web(QWebView *pg, QPrinter *printer, int n){
    34. QString html = "<html><body>";
    35. html += "<table cellspacing=0 border = 1 style='border-collapse: collapse'>";
    36. int page_height = printer->paperRect(QPrinter::Point).height();
    37. for(++n; n <= 100; n++){
    38. html += QString("<tr><td width=200>%1</td><td width=200>%2</td><td width=300>%3</td></tr>").arg(n).arg(n*n).arg(n*n*n);
    39. QString html2 = html + "</table></body></html>";
    40. pg->setHtml(html2);
    41. int content_height = pg->page()->mainFrame()->contentsSize().height();
    42. if(content_height + 20 > page_height){
    43. html += "</table>";
    44. html += QString("<p>Current value: %1</p>").arg(n);
    45. break;
    46. }
    47. }
    48. if(n > 100) html += "</table>";
    49. html += "</body></html>";
    50.  
    51. pg->setHtml(html);
    52. return n;
    53. }
    To copy to clipboard, switch view to plain text mode 

    So, I hope to get the table on the whole paper rect, except of 10-millimeter margins. But instead of it, I get something strange (SCREENSHOT). What's more - scrollbar not appears on the first page, just since second. What have I to do to fill whole page with my table and release pages from scrollbars?
    Last edited by qks1; 26th March 2013 at 09:07. Reason: sorry, wrong code. Fixed it.

Similar Threads

  1. Print is Not working by using QWebView
    By swarajbindu in forum Qt Programming
    Replies: 0
    Last Post: 21st January 2013, 10:50
  2. QWebView print() generates huge PDF files on Windows
    By brush.tyler in forum Qt Programming
    Replies: 2
    Last Post: 26th September 2012, 14:01
  3. Print a QWebView many times
    By Eddyc in forum Qt Programming
    Replies: 0
    Last Post: 28th August 2012, 10:37
  4. QWebView print to PDF and add background image
    By supergillis in forum Qt Programming
    Replies: 0
    Last Post: 9th September 2010, 13:02
  5. QWebView print() not printing as WYSIWYG
    By thiagoalencar22 in forum Qt Programming
    Replies: 1
    Last Post: 4th November 2009, 12:43

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.