if (dlg.
exec() == QDialog::Accepted) {
// calculate the total width/height table would need without scaling
const int rows = tableWidget->model()->rowCount();
const int cols = tableWidget->model()->columnCount();
double totalWidth = 0.0;
for (int c = 0; c < cols; ++c)
{
totalWidth += tableWidget->columnWidth(c);
}
double totalHeight = 0.0;
for (int r = 0; r < rows; ++r)
{
totalHeight += tableWidget->rowHeight(r);
}
// redirect table's painting on a pixmap
QPixmap pixmap
(totalWidth, totalHeight
);
QPainter::setRedirected(tableWidget
->viewport
(),
&pixmap
);
QPainter::restoreRedirected(tableWidget
->viewport
());
// print scaled pixmap
//painter.drawPixmap(printer.pageRect(), pixmap, pixmap.rect());
painter.drawPixmap(printer.pageRect().topLeft(), pixmap, pixmap.rect());
}
QPrinter printer(QPrinter::ScreenResolution);
QPrintDialog dlg(&printer, this);
if (dlg.exec() == QDialog::Accepted)
{
// calculate the total width/height table would need without scaling
const int rows = tableWidget->model()->rowCount();
const int cols = tableWidget->model()->columnCount();
double totalWidth = 0.0;
for (int c = 0; c < cols; ++c)
{
totalWidth += tableWidget->columnWidth(c);
}
double totalHeight = 0.0;
for (int r = 0; r < rows; ++r)
{
totalHeight += tableWidget->rowHeight(r);
}
// redirect table's painting on a pixmap
QPixmap pixmap(totalWidth, totalHeight);
QPainter::setRedirected(tableWidget->viewport(), &pixmap);
QPaintEvent event(QRect(0, 0, totalWidth, totalHeight));
QApplication::sendEvent(tableWidget->viewport(), &event);
QPainter::restoreRedirected(tableWidget->viewport());
// print scaled pixmap
QPainter painter(&printer);
//painter.drawPixmap(printer.pageRect(), pixmap, pixmap.rect());
painter.drawPixmap(printer.pageRect().topLeft(), pixmap, pixmap.rect());
}
To copy to clipboard, switch view to plain text mode
Bookmarks