Hi guys,

I was trying to to produce a printed copy of a QTableWidget.

Thanks to JPN I get closer to the goal but I am still experiencing
some problems..

Here is the code as it is so far:

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

The problem is that I can't get the entire table fitting in the page.

What I am doing wrong?

Thank you for your attention,

Any even partial reply is much appreciated
Roby