Results 1 to 20 of 22

Thread: Printing QTableWidget

Hybrid View

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

    Default Re: QtableWidget

    Indeed now it look grater but not all the columns fit on the page..

    "Presumably the another option is to scale according to used resolution in case it doesn't look good enough"

    Can you show me how to do it?

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

    Default Re: QtableWidget

    I tried the following:

    QPrinter printer;
    int dpi = printer.resolution();
    printer.setResolution(dpi);

    but it doesn't change the fact that the table is not entirely printed..

    Roby

  3. #3
    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: QtableWidget

    Quote Originally Posted by rmagro View Post
    Qt Code:
    1. int dpi = printer.resolution();
    2. printer.setResolution(dpi);
    To copy to clipboard, switch view to plain text mode 
    I'm sorry to say but this does nothing at all. One option is to go with QPainter::scale(). A scale factor of 0.5 makes it half of the original size, 2 makes it double.
    J-P Nurmi

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

    Default Re: Printing QTableWidget

    Many thanks JPN
    for your help!

    I tried what you suggested but it does not look good yet..

    The problem is that if I choose "painter.scale(0.5,0.5); "
    it prints the entire table but che characters inside the cells
    are not visible because of bad resolution(maybe too small)

    Further suggestions?

    Thank again

    Roby

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

    Default Re: Printing QTableWidget

    I rolled back to
    Qt Code:
    1. QPrinter printer(QPrinter::HighResolution);
    To copy to clipboard, switch view to plain text mode 

    and I added

    Qt Code:
    1. printer.setOrientation(QPrinter::Landscape);
    2. painter.scale(4,4);
    To copy to clipboard, switch view to plain text mode 

    Now it fits the page...

    but hey ...where is table bar titles??

    How to get it ??

    Thank for your reply

    Roberto.

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

    Default Re: Printing QTableWidget

    JPN,

    I tried doing this, but nothing..yet

    what is wrong with the following..

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

    Thanks for your patience,

    Roby

  7. #7
    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

    Perhaps it would be easier to convert contents of the table to HTML and use QTextDocument::print()?
    J-P Nurmi

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

    rmagro (21st June 2007)

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

    Default Re: Printing QTableWidget

    Please provide some examples,

    Thank you

    Roberto

  10. #9
    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

    What does the table contain? Plain text? Images? Could you provide a screenshot?
    J-P Nurmi

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

    Default Re: Printing QTableWidget

    Here it is..

    Then, the following to show you how I fill in the table:
    Qt Code:
    1. tableWidget->item(row, 1)->setText(tr("Verifica corretta"));
    To copy to clipboard, switch view to plain text mode 

    many thanks..
    Roby
    Attached Files Attached Files

  12. #11
    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

  13. #12
    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

  14. 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, 01:57
  2. QTableWidget issues
    By Djony in forum Qt Programming
    Replies: 42
    Last Post: 19th December 2006, 23:27
  3. print QTableWidget
    By chak_med in forum Qt Programming
    Replies: 3
    Last Post: 4th November 2006, 18:46
  4. QTableWidget editing question
    By Trasmeister in forum Qt Programming
    Replies: 1
    Last Post: 20th September 2006, 18:46
  5. Replies: 6
    Last Post: 5th March 2006, 21: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.