PDA

View Full Version : how to scale according to the used resolution



rmagro
20th June 2007, 17:38
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:


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());
}

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

wysota
3rd July 2007, 11:43
I suggest you use the window-viewport transformation of the painter. This way you'll be using logical coordinates of the pixmap but they'll be scaled to device coordinates of the printer.