J-P,
I took your 2nd option. I figured that 45 rows comfortably fit on a page. And here's what I have for the table's print method.
void PrintTableView
::printTable(QPrinter* printer,
QPainter* painter,
const QRect
& area
) {
const int rows = model()->rowCount();
const int columns = model()->columnCount();
double totalWidth = 0.0;
double totalPageHeight = 0.0;
double totalHeight = 0.0;
for (int c = 0; c < columns; ++c)
{
totalWidth += columnWidth(c);
}
for (int p = 0; p < 45; ++p)
{
totalPageHeight += rowHeight(p);
}
for (int r = 0; r<rows; ++r)
{
totalHeight += rowHeight(r);
}
const double xscale = area.width() / totalWidth;
const double yscale = area.height() / totalHeight;
const double pscale = area.height() / totalPageHeight;
painter->scale(xscale, yscale);
painter->translate(area.x() + xscale, area.y() + pscale);
bool ok;
int x=0;
for (int r = 0; r < rows; ++r) {
++x;
for (int c = 0; c < columns; ++c) {
option.rect = visualRect(idx);
itemDelegate()->paint(painter, option, idx);
}
if (x == 45){
ok = printer->newPage();
x = 0;
}
}
}
void PrintTableView::printTable(QPrinter* printer, QPainter* painter, const QRect& area)
{
const int rows = model()->rowCount();
const int columns = model()->columnCount();
double totalWidth = 0.0;
double totalPageHeight = 0.0;
double totalHeight = 0.0;
for (int c = 0; c < columns; ++c)
{
totalWidth += columnWidth(c);
}
for (int p = 0; p < 45; ++p)
{
totalPageHeight += rowHeight(p);
}
for (int r = 0; r<rows; ++r)
{
totalHeight += rowHeight(r);
}
const double xscale = area.width() / totalWidth;
const double yscale = area.height() / totalHeight;
const double pscale = area.height() / totalPageHeight;
painter->scale(xscale, yscale);
painter->translate(area.x() + xscale, area.y() + pscale);
bool ok;
int x=0;
for (int r = 0; r < rows; ++r) {
++x;
for (int c = 0; c < columns; ++c) {
QModelIndex idx = model()->index(r,c);
QStyleOptionViewItem option = viewOptions();
option.rect = visualRect(idx);
itemDelegate()->paint(painter, option, idx);
}
if (x == 45){
ok = printer->newPage();
x = 0;
}
}
}
To copy to clipboard, switch view to plain text mode
What winds up happening is that if I have the following:
painter->scale(xscale, yscale);
painter->translate(area.x() + xscale, area.y() + pscale);
painter->scale(xscale, yscale);
painter->translate(area.x() + xscale, area.y() + pscale);
To copy to clipboard, switch view to plain text mode
this prints out fine exept the text is EXTREMELY small. When I swap pscale and yscale, as in:
painter->scale(xscale, pscale);
painter->translate(area.x() + xscale, area.y() + yscale);
painter->scale(xscale, pscale);
painter->translate(area.x() + xscale, area.y() + yscale);
To copy to clipboard, switch view to plain text mode
the first page prints out fine (rows 1 - 45), and then the printer spits out x more blank pages to cover the rest of the rows. Do I need to re-establish the QStyleOptionViewItem for every newPage()? I'm really puzzled why the printing works with when scaled really small, but when scaled to 45 rows per page, only the first page contains text. Any ideas?
Thanks!
--Derrick
Bookmarks