Results 1 to 10 of 10

Thread: experiencing problem at printing out QTableWidget

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

    Angry experiencing problem at printing out 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. #2
    Join Date
    May 2006
    Location
    Germany
    Posts
    108
    Thanks
    2
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: experiencing problem at printing out QTableWidget

    I don't see where you paint the header? You're only printing the rows of the table, while the horizontal- and verticalHeaders are neither rows, nor columns.

    For simplification you could use the render() functionality to easily print your tablewidget into a pixmap.
    "If you lie to the compiler, it will get its revenge." - Henry Spencer

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

    Default Re: experiencing problem at printing out QTableWidget

    Thanks Methedrine for your reply,

    Indeed I did not print the header because I didn't know how to do it combined with the rows.

    Then, thanks also for the tips tou gave me about the render()..
    Would you be so kind to some examples ?

    Roby

  4. #4
    Join Date
    May 2006
    Location
    Germany
    Posts
    108
    Thanks
    2
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: experiencing problem at printing out QTableWidget

    Quote Originally Posted by rmagro View Post
    Thanks Methedrine for your reply,

    Indeed I did not print the header because I didn't know how to do it combined with the rows.

    Then, thanks also for the tips tou gave me about the render()..
    Would you be so kind to some examples ?

    Roby
    See the docs, there's an example on what you need:
    QWidget::render()
    "If you lie to the compiler, it will get its revenge." - Henry Spencer

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

    Default Re: experiencing problem at printing out QTableWidget

    but QWidget::render() is not a member of QTableWidget

    and I need to print a QTableWidget..

    Thank you,

    Roberto.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: experiencing problem at printing out QTableWidget

    Quote Originally Posted by rmagro View Post
    but QWidget::render() is not a member of QTableWidget
    QTableWidget is a QWidget too. Although that method was introduced in Qt 4.3, so if you use some earlier Qt version, you can't use it.

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

    Default Re: experiencing problem at printing out QTableWidget

    I am using QT 4 and render is not a render is not a member of QWidget anymore..

    any suggestions about how to get the point using the first code I posted?

    Thank you,

    Roby

  8. #8
    Join Date
    May 2006
    Location
    Germany
    Posts
    108
    Thanks
    2
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: experiencing problem at printing out QTableWidget

    Quote Originally Posted by rmagro View Post
    I am using QT 4 and render is not a render is not a member of QWidget anymore..
    Precisely, you are using a Qt 4 version < 4.3.0, as such, render is not yet a member of QWidget

    Quote Originally Posted by rmagro View Post
    any suggestions about how to get the point using the first code I posted?

    Thank you,

    Roby
    Just fetch the horizontalHeader() from QTableWidget, get its height and draw it onto the pixmap before you draw the rows you have.
    "If you lie to the compiler, it will get its revenge." - Henry Spencer

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

    Default Re: experiencing problem at printing out QTableWidget

    I did like this ...

    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.  
    12. double totalWidth = 0.0;
    13.  
    14. for (int c = 0; c < cols; ++c)
    15. {
    16. totalWidth += tableWidget->columnWidth(c);
    17. }
    18.  
    19. double totalHeight = tableWidget->horizontalHeader()->height();
    20.  
    21. for (int r = 0; r < rows; ++r)
    22. {
    23. totalHeight += tableWidget->rowHeight(r);
    24. }
    25.  
    26.  
    27. // redirect table's painting on a pixmap
    28. QPixmap pixmap(totalWidth, totalHeight );
    29.  
    30. QPainter::setRedirected(tableWidget->horizontalHeader()->viewport(), &pixmap);
    31. QPainter::setRedirected(tableWidget->viewport(), &pixmap);
    32.  
    33. QPaintEvent event(QRect(0, 0, totalWidth, totalHeight ));
    34.  
    35. QApplication::sendEvent(tableWidget->horizontalHeader()->viewport(), &event);
    36. QApplication::sendEvent(tableWidget->viewport(), &event);
    37.  
    38. QPainter::restoreRedirected(tableWidget->horizontalHeader()->viewport());
    39. QPainter::restoreRedirected(tableWidget->viewport());
    40.  
    41.  
    42.  
    43. // print scaled pixmap
    44. QPainter painter(&printer);
    45. painter.scale(4,4);
    46. //painter.drawPixmap(printer.pageRect(), pixmap, pixmap.rect());
    47. painter.drawPixmap(printer.pageRect().topLeft(), pixmap, pixmap.rect());
    48. }
    To copy to clipboard, switch view to plain text mode 

    but I do not get the point..

    What's wrong with that code??

    Thank you for your patience

    Roby

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

    Default Re: experiencing problem at printing out QTableWidget

    If I change the order of the instructions in the section
    " // redirect table's painting on a pixmap "
    I get both the horizontal header and the rows printed out BUT they are overlapped..

    How to adjust the situation??

    Qt Code:
    1. // redirect table's painting on a pixmap
    2. QPixmap pixmap(totalWidth, totalHeight);
    3.  
    4.  
    5. QPainter::setRedirected(tableWidget->viewport(), &pixmap);
    6. QPainter::setRedirected(tableWidget->horizontalHeader()->viewport(), &pixmap);
    7.  
    8. QPaintEvent event(QRect(0, 0, totalWidth, totalHeight));
    9.  
    10.  
    11. QApplication::sendEvent(tableWidget->viewport(), &event);
    12. QApplication::sendEvent(tableWidget->horizontalHeader()->viewport(), &event);
    13.  
    14. QPainter::restoreRedirected(tableWidget->viewport());
    15. QPainter::restoreRedirected(tableWidget->horizontalHeader()->viewport());
    16.  
    17.  
    18.  
    19.  
    20. // print scaled pixmap
    21. QPainter painter(&printer);
    22. painter.scale(4,4);
    23. //painter.drawPixmap(printer.pageRect(), pixmap, pixmap.rect());
    24. painter.drawPixmap(printer.pageRect().topLeft(), pixmap, pixmap.rect());
    To copy to clipboard, switch view to plain text mode 

    Thanks for help,

    Roby

Similar Threads

  1. Printing QTableWidget
    By rmagro in forum Qt Programming
    Replies: 21
    Last Post: 27th June 2007, 21:06
  2. QTableWidget Problem, setRowWidget? :P
    By VireX in forum Newbie
    Replies: 17
    Last Post: 6th April 2007, 18:12
  3. Problem inserting in QTableWidget
    By DPinLV in forum Qt Programming
    Replies: 2
    Last Post: 2nd August 2006, 00: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.