printer.
setOrientation(QPrinter::Landscape);
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 = tableWidget->horizontalHeader()->height();
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
->horizontalHeader
()->viewport
(),
&pixmap
);
QPainter::setRedirected(tableWidget
->viewport
(),
&pixmap
);
QApplication::sendEvent(tableWidget
->horizontalHeader
()->viewport
(),
&event
);
QPainter::restoreRedirected(tableWidget
->horizontalHeader
()->viewport
());
QPainter::restoreRedirected(tableWidget
->viewport
());
// print scaled pixmap
painter.scale(4,4);
//painter.drawPixmap(printer.pageRect(), pixmap, pixmap.rect());
painter.drawPixmap(printer.pageRect().topLeft(), pixmap, pixmap.rect());
}
QPrinter printer(QPrinter::HighResolution);
printer.setOrientation(QPrinter::Landscape);
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 = tableWidget->horizontalHeader()->height();
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->horizontalHeader()->viewport(), &pixmap);
QPainter::setRedirected(tableWidget->viewport(), &pixmap);
QPaintEvent event(QRect(0, 0, totalWidth, totalHeight ));
QApplication::sendEvent(tableWidget->horizontalHeader()->viewport(), &event);
QApplication::sendEvent(tableWidget->viewport(), &event);
QPainter::restoreRedirected(tableWidget->horizontalHeader()->viewport());
QPainter::restoreRedirected(tableWidget->viewport());
// print scaled pixmap
QPainter painter(&printer);
painter.scale(4,4);
//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