Page 2 of 2 FirstFirst 12
Results 21 to 22 of 22

Thread: Printing QTableWidget

  1. #21
    Join Date
    Jun 2007
    Location
    italy
    Posts
    126
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    15
    Thanked 1 Time in 1 Post

    Question Re: Printing QTableWidget

    Hi Qt Community,

    I'm using the following code to print out a QTableWidget,
    but I only get the the contents inside the table and not the horizontalHeader..

    Qt Code:
    1. QPrinter printer(QPrinter::HighResolution);
    2. printer.setOrientation(QPrinter::Landscape);
    3.  
    4. QPrintDialog dlg(&printer, this);
    5.  
    6. if (dlg.exec() == QDialog::Accepted)
    7. {
    8. // calculate the total width/height table would need without scaling
    9. const int rows = tableWidget->model()->rowCount();
    10. const int cols = tableWidget->model()->columnCount();
    11. double totalWidth = 0.0;
    12.  
    13. for (int c = 0; c < cols; ++c)
    14. {
    15. totalWidth += tableWidget->columnWidth(c);
    16. }
    17.  
    18. double totalHeight = 0.0;
    19.  
    20. for (int r = 0; r < rows; ++r)
    21. {
    22. totalHeight += tableWidget->rowHeight(r);
    23. }
    24.  
    25. // redirect table's painting on a pixmap
    26. QPixmap pixmap(totalWidth, totalHeight);
    27. QPainter::setRedirected(tableWidget->viewport(), &pixmap);
    28. QPaintEvent event(QRect(0, 0, totalWidth, totalHeight));
    29. QApplication::sendEvent(tableWidget->viewport(), &event);
    30. QPainter::restoreRedirected(tableWidget->viewport());
    31.  
    32. // print scaled pixmap
    33. QPainter painter(&printer);
    34. painter.scale(4,4);
    35. //painter.drawPixmap(printer.pageRect(), pixmap, pixmap.rect());
    36. painter.drawPixmap(printer.pageRect().topLeft(), pixmap, pixmap.rect());
    37. }
    To copy to clipboard, switch view to plain text mode 

    What to add to get it?

    Thanks you very much for your help,

    Roby

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

    Default Re: Printing QTableWidget

    Quote Originally Posted by rmagro View Post
    Here it is..
    Alright, so the table contains multi line text and icons. What do you think about something like this?
    Attached Files Attached Files
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    janus (18th April 2008)

Similar Threads

  1. QTableWidget (resizing rows, turning off selection, etc.)
    By kiss-o-matic in forum Qt Programming
    Replies: 6
    Last Post: 11th January 2007, 02:57
  2. QTableWidget issues
    By Djony in forum Qt Programming
    Replies: 42
    Last Post: 20th December 2006, 00:27
  3. print QTableWidget
    By chak_med in forum Qt Programming
    Replies: 3
    Last Post: 4th November 2006, 19:46
  4. QTableWidget editing question
    By Trasmeister in forum Qt Programming
    Replies: 1
    Last Post: 20th September 2006, 19:46
  5. Replies: 6
    Last Post: 5th March 2006, 22:05

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
  •  
Qt is a trademark of The Qt Company.