Results 1 to 6 of 6

Thread: problem printing a 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

    Question problem printing a QTableWidget

    Hi all,
    the code that follows stop working correctly when I upgrade from Qt4.2.2 to Qt4.4.3..
    The only think printed is a bold black line..

    Any clue about how I can solve?

    Thx very mutch

    Qt Code:
    1. QPrinter printer(QPrinter::HighResolution);
    2. printer.setOrientation(QPrinter::Landscape);
    3.  
    4. QTableWidget *newTableWidget = tableWidget;
    5.  
    6. QPrintDialog dlg(&printer, this);
    7.  
    8. if (dlg.exec() == QDialog::Accepted)
    9. {
    10. // calculate the total width/height table would need without scaling
    11. const int rows = newTableWidget ->model()->rowCount();
    12. const int cols = newTableWidget ->model()->columnCount();
    13.  
    14. double totalWidth = 0.0;
    15.  
    16. for (int c = 0; c < cols; ++c)
    17. {
    18. totalWidth += newTableWidget ->columnWidth(c);
    19. }
    20.  
    21. double totalHeight = 0.0;
    22.  
    23. for (int r = 0; r < rows; ++r)
    24. {
    25. totalHeight += newTableWidget ->rowHeight(r);
    26. }
    27.  
    28. double totalHeight1;
    29. int num_page = 1;
    30.  
    31. while (totalHeight > 900)
    32. {
    33. totalHeight1 = 900;
    34.  
    35. // redirect table's painting on a pixmap
    36. QPixmap pixmap(totalWidth, newTableWidget->horizontalHeader()->height());
    37. QPixmap pixmap1(totalWidth, totalHeight1);
    38.  
    39. QPaintEvent event(QRect(0, 0, totalWidth, newTableWidget->horizontalHeader()->height()));
    40. QPaintEvent event1(QRect(0, 0, totalWidth, totalHeight1));
    41.  
    42. QPainter::setRedirected(newTableWidget->horizontalHeader()->viewport(), &pixmap);
    43. QApplication::sendEvent(newTableWidget->horizontalHeader()->viewport(), &event);
    44. QPainter::restoreRedirected(newTableWidget->horizontalHeader()->viewport());
    45.  
    46. QPainter::setRedirected(newTableWidget->viewport(), &pixmap1);
    47. QApplication::sendEvent(newTableWidget->viewport(), &event1);
    48. QPainter::restoreRedirected(newTableWidget->viewport());
    49.  
    50. // print scaled pixmap
    51. QPainter painter(&printer);
    52.  
    53. QDateTime dataDiOggi = QDateTime::currentDateTime();
    54. QString dataDiOggiStringa = dataDiOggi.toString("dd.MM.yyyy hh:mm:ss");
    55.  
    56. painter.drawText(0, 250, tr("Qt 4.3.3 - Date: %1 -") .arg(dataDiOggiStringa));
    57. painter.drawText(0, 150, tr("Page %1") .arg(num_page));
    58.  
    59. painter.scale(4.5,4.5);
    60. painter.drawPixmap(0, 100, totalWidth, newTableWidget->horizontalHeader()->height(), pixmap);
    61. painter.drawPixmap(0, 100+newTableWidget->horizontalHeader()->height(), totalWidth, totalHeight1, pixmap1);
    62.  
    63. totalHeight = totalHeight - totalHeight1;
    64. printer.newPage();
    65. numero_pagina = numero_pagina + 1;
    66. }
    67.  
    68. QPixmap pixmap(totalWidth, newTableWidget->horizontalHeader()->height());
    69. QPixmap pixmap1(totalWidth, totalHeight);
    70.  
    71. QPaintEvent event(QRect(0, 0, totalWidth, newTableWidget->horizontalHeader()->height()));
    72. QPaintEvent event1(QRect(0, 0, totalWidth, totalHeight));
    73.  
    74. QPainter::setRedirected(newTableWidget->horizontalHeader()->viewport(), &pixmap);
    75. QApplication::sendEvent(newTableWidget->horizontalHeader()->viewport(), &event);
    76. QPainter::restoreRedirected(newTableWidget->horizontalHeader()->viewport());
    77.  
    78. QPainter::setRedirected(newTableWidget->viewport(), &pixmap1);
    79. QApplication::sendEvent(newTableWidget->viewport(), &event1);
    80. QPainter::restoreRedirected(newTableWidget->viewport());
    81.  
    82. // print scaled pixmap
    83. QPainter painter(&printer);
    84.  
    85. QDateTime dataDiOggi = QDateTime::currentDateTime();
    86. QString dataDiOggiStringa = dataDiOggi.toString("dd.MM.yyyy hh:mm:ss");
    87.  
    88. painter.drawText(0, 250, tr("Qt 4.3.3 - Date: %1 - ") .arg(dataDiOggiStringa));
    89. painter.drawText(0, 150, tr("Page %1") .arg(num_page));
    90.  
    91. painter.scale(4.5,4.5);
    92. painter.drawPixmap(0, 100, totalWidth, newTableWidget->horizontalHeader()->height(), pixmap);
    93. painter.drawPixmap(0, 100+newTableWidget->horizontalHeader()->height(), totalWidth, totalHeight, pixmap1);
    94. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: problem printing a QTableWidget

    With out looking to much in to the code, if this is NOT in a paintEvent(), this should not have worked on Qt4.X at all.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  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: problem printing a QTableWidget

    Surely QPaintEvent is used
    Qt Code:
    1. QPaintEvent event(QRect(0, 0, totalWidth, newTableWidget->horizontalHeader()->height()));
    2. QPaintEvent event1(QRect(0, 0, totalWidth, totalHeight));
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: problem printing a QTableWidget

    EDIT
    Deleted
    Last edited by high_flyer; 29th December 2009 at 10:32.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  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: problem printing a QTableWidget

    Any help??
    with Qt 4.2.2 the code works good. With higher version it stops working correctly. I mean it does not print QtableWidget but only a blod black line..
    Thx
    Roby

  6. #6
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: problem printing a QTableWidget

    I didn't inspect your code much but I imagine you use paint events to paint table widget on a pixmap? If so then just use QWidget::render() - much more simple, so much less space for mistake.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  7. The following user says thank you to faldzip for this useful post:

    rmagro (11th January 2010)

Similar Threads

  1. Replies: 6
    Last Post: 22nd November 2016, 13:57
  2. Replies: 0
    Last Post: 9th February 2009, 20:57
  3. QTableWidget problem
    By MarkoSan in forum Qt Programming
    Replies: 53
    Last Post: 7th May 2008, 11:15
  4. problem in printing QTableView
    By miguel_mark in forum Qt Programming
    Replies: 2
    Last Post: 6th February 2008, 08:19
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.