Yet another (maybe even better?) option is to use QPainter's powerful redirecting mechanism. This approach doesn't even require subclassing of QTableView.

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