Results 1 to 1 of 1

Thread: QTableView printing(Qt Commercial Support Weekly #25 – Printing large tables)

  1. #1
    Join Date
    Aug 2008
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTableView printing(Qt Commercial Support Weekly #25 – Printing large tables)

    I am trying to use the code snippets as in
    https://translate.googleusercontent....vW1SwcjvG1gKiA

    Unfornunatly the example link http://83.145.193.200/Global/Images/...weekly_25_.zip is invalid.

    Have you ever had problems printing a large table? Every now and then is approached with media issues is how to accomplish achieve this. The problem comes When one: has a QTableView and it's larger than what is shown on the screen. Using grabWidget () method is the general way to go about this, how do we aim wears it so que la pixmap created can be used for printing?
    This is exactly what I want to do, print a spreadsheet, but could not get the code functional.

    I tried to make this working with the following class, and just instantiate from the ui where I want to print the tableView:

    Qt Code:
    1. #ifndef TABLE_PRIINTER_H
    2. #define TABLE_PRIINTER_H
    3.  
    4. #include <QMainWindow>
    5. #include <QObject>
    6. #include "QTableWidget"
    7. #include <QPrinter>
    8. #include <QPrintDialog>
    9.  
    10. class table_printer : public QMainWindow
    11. {
    12. Q_OBJECT
    13. public:
    14. explicit table_printer(QWidget *parent = 0);
    15.  
    16. void print_table (QTableWidget *tableWidget);
    17. signals:
    18.  
    19. public slots:
    20. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "table_printer.h"
    2. #include <QHeaderView>
    3. #include <qpainter.h>
    4.  
    5. table_printer::table_printer(QWidget *parent) : QMainWindow(parent)
    6. {
    7.  
    8. }
    9. /** ---------------------------------------------------------------------------
    10.  * @brief table_printer::print_table
    11.  * @param tableWidget
    12.  */
    13. void table_printer::print_table (QTableWidget * tableWidget)
    14. {
    15. //QPrinter * printer = new QPrinter;
    16. QPrinter * printer = new QPrinter(QPrinter::HighResolution);
    17.  
    18. QPrintDialog printer_dialog(printer);
    19.  
    20. printer->setOutputFileName("altium_bom.pdf");
    21. if (printer_dialog.exec() == QDialog::Accepted)
    22. {
    23. printer->setPaperSize(QPrinter::A3);
    24. printer->setFullPage(true);
    25. printer->setOrientation(QPrinter::Landscape);
    26. QPainter painter(printer);
    27.  
    28. const int rows = tableWidget->model()->rowCount();
    29. const int columns = tableWidget->model()->columnCount();
    30. double totalWidth = tableWidget->verticalHeader()->width();
    31.  
    32. for (int c = 0; c < columns; ++c)
    33. totalWidth += tableWidget->columnWidth(c);
    34.  
    35. double totalHeight = tableWidget->horizontalHeader()->height();
    36.  
    37. for (int r = 0; r < rows; ++r)
    38. totalHeight += tableWidget->rowHeight(r);
    39.  
    40. // To avoid possible visual updates we will use a temporary
    41. // QTableView which is not shown on the screen which we can
    42. // ensure with the WA_DontShowOnScreen attribute.
    43. // We will set the size of the table based on the width and
    44. // height we just calculated. Also we will make sure that scrollbars
    45. // are not shown for the table.
    46. // Then we can use the grabWidget() method to paint the table.
    47.  
    48.  
    49. QTableView tempTable;
    50. tempTable.setAttribute(Qt::WA_DontShowOnScreen);
    51. tempTable.setModel(tableWidget->model());
    52. tempTable.setFixedSize(totalWidth, totalHeight);
    53. tempTable.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    54. tempTable.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    55. QPixmap grabPixmap = QPixmap::grabWidget(&tempTable);
    56.  
    57.  
    58. // Now we have our table widget with all the items and headers painted to a pixmap.
    59. // If we try to print this pixmap, then what will most likely happen is that the
    60. // columns or the rows are not evenly divided between pages.
    61. // Some of them may be cut in half. This makes reading the printed table very
    62. // difficult. We have to print the table so that only full rows and
    63. // columns are printed to each page.
    64.  
    65. // What we will do to help resolve this is to use a helper rectangle to
    66. // define the area from the pixmap that is printed into one page.
    67. // To make this example straightforward to read we will define the
    68. // amount of columns and rows printed.
    69. // This can easily be changed so that the width and height is compared
    70. // to the actual page size.
    71.  
    72. // First we will take the first four rows and columns (along with the headers)
    73. // and calculate the space they occupy.
    74. // The helper rectangle is used for this. Then we will use this rectangle
    75. // to take the correct part of the grabbed pixmap for printing.
    76. // Needed variables are initialized and then the size of the rectangle
    77. // is added up.
    78.  
    79. // The fact that the horizontal header is only printed with the top most items
    80. // and the vertical header with the left most items is taken into account.
    81.  
    82.  
    83. QRectF sourceRect;
    84. double totalPageHeight = tableWidget->horizontalHeader()->height();
    85. int columnCount = 0;
    86. int rowCount = 0;
    87.  
    88. // First take the rows that fit into one page
    89. for (int h = 0; h < tableWidget->model()->rowCount(); h++)
    90. {
    91. totalPageHeight += tableWidget->rowHeight(h);
    92. double totalPageWidth = tableWidget->verticalHeader()->width();
    93.  
    94. // When we have reached the amount of rows we will print into one page,
    95. // the width of the columns we want into that page is defined.
    96.  
    97. if (rowCount == 3 || h == tableWidget->model()->rowCount() - 1)
    98. {
    99. // Then take the columns that fit into one page
    100. for (int w = 0; w < tableWidget->model()->columnCount(); w++)
    101. {
    102. totalPageWidth += tableWidget->columnWidth(w);
    103.  
    104. // Now that we have determined the size of the rows and
    105. // columns, the size is set to the rectangle we use for printing.
    106. // Now the first part of the table is ready for printing.
    107.  
    108. if ( (columnCount == 3) ||
    109. (w == tableWidget->model()->columnCount() - 1))
    110. {
    111. sourceRect.setWidth(totalPageWidth);
    112. sourceRect.setHeight(totalPageHeight);
    113. painter.drawPixmap(printer->pageRect().topLeft(),
    114. grabPixmap, // pixmap,
    115. sourceRect);
    116. // After printing the first page we have to adjust the
    117. // rectangle so that it’s moved to the next part of the
    118. // table we want to print. To be able to print the next
    119. // area to a new page, a page break is added and the page
    120. // width and column count are reset.
    121. // This way we can continue on to the next page and start
    122. // going through the columns from the beginning.
    123.  
    124. sourceRect.setLeft(sourceRect.left() + totalPageWidth);
    125. if (w != tableWidget->model()->columnCount() - 1)
    126. printer->newPage();
    127. totalPageWidth = 0;
    128. columnCount = 0;
    129.  
    130. // A page break is also needed after all the rows in one page
    131. // have been counted in. As with the rows, the rectangle is
    132. // adjusted to the next part of the table and the page
    133. // height is reset. We will cover all the parts of the
    134. // pixmap this way until the whole table is printed out.
    135.  
    136. sourceRect.setTop(sourceRect.top() + totalPageHeight);
    137. sourceRect.setLeft(0);
    138. if (h != tableWidget->model()->rowCount() - 1)
    139. printer->newPage();
    140. totalPageHeight = 0;
    141. rowCount = 0;
    142. }
    143. }
    144. }
    145. }
    146. tableWidget->render(&painter);
    147. }
    148. }
    To copy to clipboard, switch view to plain text mode 
    This works but I get two images, either very very small (double image), or if I remove 'hi-resolution' only the first page with another one at the back of it, as one would get with a normal print-screen. I attach a sample of low and high resolution 'printouts'.

    Normal print-screen was done with
    Qt Code:
    1. QPrintDialog printer_dialog(printer);
    2.  
    3. if (printer_dialog.exec() == QDialog::Accepted)
    4. {
    5. printer->setPaperSize(QPrinter::A3);
    6. printer->setFullPage(true);
    7. printer->setOrientation(QPrinter::Landscape);
    8.  
    9. QPainter painter(printer);
    10. ui.tw_price_list->render(&painter);
    11. }
    To copy to clipboard, switch view to plain text mode 
    This could be very use full class if one could fix this.

    Any Ideas?

    Thanks
    Attached Files Attached Files

Similar Threads

  1. Replies: 0
    Last Post: 29th April 2013, 11:47
  2. QTableView printing
    By derrickbj in forum Qt Programming
    Replies: 24
    Last Post: 23rd May 2012, 08:57
  3. Printing from QTableView
    By baray98 in forum Qt Programming
    Replies: 0
    Last Post: 18th June 2010, 23:34
  4. problem in printing QTableView
    By miguel_mark in forum Qt Programming
    Replies: 2
    Last Post: 6th February 2008, 08:19
  5. large tables and QSqlTableModel
    By peter in forum Qt Programming
    Replies: 0
    Last Post: 15th October 2006, 21:18

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.