Results 1 to 2 of 2

Thread: How to print a QTableView

  1. #1
    Join Date
    Mar 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default How to print a QTableView

    Hi,

    I would like to print a table which contains text only. I search through the forum and found this piece of code

    Qt Code:
    1. QPainter painter(printer);
    2. const QRect area = printer->pageRect();
    3. const int rows = m_data_model.rowCount();
    4. const int cols = m_data_model.columnCount();
    5.  
    6. // calculate the total width/height table would need without scaling
    7. double totalWidth = 0.0;
    8. for (int c = 0; c < cols; ++c)
    9. {
    10. totalWidth += columnWidth(c);
    11. }
    12. double totalHeight = 0.0;
    13. for (int r = 0; r < rows; ++r)
    14. {
    15. totalHeight += rowHeight(r);
    16. }
    17.  
    18. // paint cells
    19. for (int r = 0; r < rows; ++r)
    20. {
    21. for (int c = 0; c < cols; ++c)
    22. {
    23. QModelIndex idx = m_data_model.index(r, c);
    24. QStyleOptionViewItem option = viewOptions();
    25. option.rect = visualRect(idx);
    26. itemDelegate()->paint(&painter, option, idx);
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 

    However, the code does not print the table's vertical and horizontal headers, as well as the grid lines. How can I print a table, with both the headers and the grid lines?

    I am using Qt4.4.

    Thanks.

  2. #2
    Join Date
    Mar 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to print a QTableView

    Now, I am able to print the horizontal header with the code below:

    Qt Code:
    1. const int horizontal_height = horizontalHeader()->height();
    2. const int vertical_width = verticalHeader()->width();
    3. int width = horizontal_header_rect.size().width();
    4.  
    5. QWidget *view_port = horizontalHeader()->viewport();
    6. QPixmap horizontal_pixmap(width, horizontal_height);
    7. QPaintEvent paint_horizontal_event(horizontal_header_rect);
    8. dc->setRedirected(view_port, &horizontal_pixmap);
    9. QApplication::sendEvent(view_port, &paint_horizontal_event);
    10. dc->restoreRedirected(view_port);
    11. dc->drawPixmap(printer->pageRect().topLeft() + QPoint(vertical_width, 0), horizontal_pixmap, horizontal_header_rect);
    To copy to clipboard, switch view to plain text mode 

    It could print nicely but, I still face a problem if the header is larger than the paper size. For example, I have a table with 33 columns. I could print nicely the header for the first 20 columns in first page, but fails to print for the remaining 13 columns in second page.

    How could I modify the code to print the header nicely for the second page and all subsequent pages? Could any gurus out there helps?

    Thanks.

Similar Threads

  1. Very high CPU usage with QTableView and MVC
    By montylee in forum Qt Programming
    Replies: 7
    Last Post: 24th March 2009, 06:14
  2. QTableView
    By dragon in forum Qt Programming
    Replies: 0
    Last Post: 22nd September 2008, 16:53
  3. Change margin on QTextEdit print()
    By Dii in forum Qt Programming
    Replies: 2
    Last Post: 20th May 2008, 22:45
  4. make QTableView work as a multi-column list view
    By wesley in forum Qt Programming
    Replies: 1
    Last Post: 11th March 2008, 14:43
  5. Multi-line messages in QTableView
    By Conel in forum Qt Programming
    Replies: 6
    Last Post: 13th April 2006, 13:49

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.