printer.
setOrientation(QPrinter::Landscape);
if (dlg.
exec() == QDialog::Accepted) {
// calculate the total width/height table would need without scaling
const int rows = ui->Packet_Info->model()->rowCount();
const int cols = ui->Packet_Info->model()->columnCount();
double totalWidth = 0.0;
for (int c = 0; c < cols; ++c)
{
totalWidth += ui->Packet_Info->columnWidth(c);
}
double totalHeight = ui->Packet_Info->horizontalHeader()->height();
for (int r = 0; r < rows; ++r)
{
totalHeight += ui->Packet_Info->rowHeight(r);
}
// redirect table's painting on a pixmap
QPixmap pixmap
(totalWidth, totalHeight
);
QPainter::setRedirected(ui
->Packet_Info
->horizontalHeader
()->viewport
(),
&pixmap
);
QPainter::setRedirected(ui
->Packet_Info
->viewport
(),
&pixmap
);
QApplication::sendEvent(ui
->Packet_Info
->horizontalHeader
()->viewport
(),
&event
);
QApplication::sendEvent(ui
->Packet_Info
->viewport
(),
&event
);
QPainter::restoreRedirected(ui
->Packet_Info
->horizontalHeader
()->viewport
());
QPainter::restoreRedirected(ui
->Packet_Info
->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 = ui->Packet_Info->model()->rowCount();
const int cols = ui->Packet_Info->model()->columnCount();
double totalWidth = 0.0;
for (int c = 0; c < cols; ++c)
{
totalWidth += ui->Packet_Info->columnWidth(c);
}
double totalHeight = ui->Packet_Info->horizontalHeader()->height();
for (int r = 0; r < rows; ++r)
{
totalHeight += ui->Packet_Info->rowHeight(r);
}
// redirect table's painting on a pixmap
QPixmap pixmap(totalWidth, totalHeight );
QPainter::setRedirected(ui->Packet_Info->horizontalHeader()->viewport(), &pixmap);
QPainter::setRedirected(ui->Packet_Info->viewport(), &pixmap);
QPaintEvent event(QRect(0, 0, totalWidth, totalHeight ));
QApplication::sendEvent(ui->Packet_Info->horizontalHeader()->viewport(), &event);
QApplication::sendEvent(ui->Packet_Info->viewport(), &event);
QPainter::restoreRedirected(ui->Packet_Info->horizontalHeader()->viewport());
QPainter::restoreRedirected(ui->Packet_Info->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