J-P,
Thanks for the reply. I implemented your suggestion, and I'm still getting a blank page.![]()
J-P,
Thanks for the reply. I implemented your suggestion, and I'm still getting a blank page.![]()
Qt Code:
{ const int rows = model()->rowCount(); const int cols = model()->columnCount(); // calculate the total width/height table would need without scaling double totalWidth = 0.0; for (int c = 0; c < cols; ++c) { totalWidth += columnWidth(c); } double totalHeight = 0.0; for (int r = 0; r < rows; ++r) { totalHeight += rowHeight(r); } // calculate proper scale factors const double scaleX = area.width() / totalWidth; const double scaleY = area.height() / totalHeight; painter->scale(scaleX, scaleY); // paint cells for (int r = 0; r < rows; ++r) { for (int c = 0; c < cols; ++c) { option.rect = visualRect(idx); itemDelegate()->paint(painter, option, idx); } } } // printer usage tableView->print(&painter, printer.pageRect()); // test on pixmap tableView->print(&painter, pixmap.rect()); pixmap.save("table.png", "PNG");To copy to clipboard, switch view to plain text mode
Edit: I forgot to add painter translating but that should be trivial to add once you get the idea..
Last edited by jpn; 29th September 2006 at 21:24.
J-P Nurmi
Yet another (maybe even better?) option is to use QPainter's powerful redirecting mechanism. This approach doesn't even require subclassing of QTableView.
Qt Code:
{ // calculate the total width/height table would need without scaling const int rows = table->model()->rowCount(); const int cols = table->model()->columnCount(); double totalWidth = 0.0; for (int c = 0; c < cols; ++c) { totalWidth += table->columnWidth(c); } double totalHeight = 0.0; for (int r = 0; r < rows; ++r) { totalHeight += table->rowHeight(r); } // redirect table's painting on a pixmap // print scaled pixmap painter.drawPixmap(printer.pageRect(), pixmap, pixmap.rect()); }To copy to clipboard, switch view to plain text mode
J-P Nurmi
J-P,
This is AWESOME!!! Thank you so much! both of these work well. The first suggestion works nicely and scales the text well. The 2nd option prints only to the printer (i.e., won't print to Adobe PDF's printer - for instance, i get a "Critical: QPixmap::toWinHBITMAP(), failed to create dibsection(Not enough stoarge is available to process this command.) but that's a hurdle to tackle another time) but prints the grid lines, which is REALLY nice. One issue I have with both suggestions, however, is that for large tables (more than 1 page), it scales the text to fit onto one page. I had thought to emit a signal to call QPrinter::newPage(), but, I can't seem to do this with the QPainter you've set up.
I've also tried :
Qt Code:
double totalHeight = 0.0; for (int r = 0; r < 45; ++r){ totalHeight += table->rowHeight(r); }To copy to clipboard, switch view to plain text mode
for your examples, but this only prints 1 page as well. Do you know how I can tell QPainter to send a new page request to the Printer?
Again...Thank you very much for your help!!!
--D
I suppose you have two options. Either you will have to do some calculation before hand so that you know how many rows and columns fit to one page and then request the table to paint a certain range of rows and columns to the specified area. Another option is to pass the whole QPrinter instead of just QPainter and do the calculations inside table's printing method where QPrinter::newPage() is invoked when appropriate.
J-P Nurmi
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.
Qt Code:
{ 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; } } }To copy to clipboard, switch view to plain text mode
What winds up happening is that if I have the following:
this prints out fine exept the text is EXTREMELY small. When I swap pscale and yscale, as in:Qt Code:
painter->scale(xscale, yscale); painter->translate(area.x() + xscale, area.y() + pscale);To copy to clipboard, switch view to plain text mode
Qt Code:
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
Maybe you need to save the QPainter always before scaling and translating and restore it afterwards. You are using the same QPainter object for painting all pages, this means you are also scaling it again and again.
J-P Nurmi
hmmm..I'm not exactly sure. Here's what I tried based on your idea...
Qt Code:
{ 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); painter->save(); 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; painter->restore(); } } }To copy to clipboard, switch view to plain text mode
if I put painter->save() before the painter->scale(), it prints every page, but only in a very very small square. if I leave it the way it is shown here, it prints every page stretched to page width, but compressed to 1/8th the page. If I do painter->scale(xscale, pscale),and leave painter->save() after painter->translate(), then it prints the first page correctly, but all other pages are blank. I tried re-scaling the painter when x == 45, but that only hangs the program.![]()
Ok, sorry for not paying enough attention. Forget about saving and restoring for now. Seems I didn't read your code carefully enough. I somehow thought you were calling the print function for every page...
What I'm suspecting is that the areas returned by the visualRect(idx) don't match on the second page because it's not aware of that you have requested a new page and the coordinates have been reseted. So you would have to translate the painter in vertical direction after every new page request. Try to print the pageRect and the visualRects in the debug output or something and you'll see.
J-P Nurmi
derrickbj (10th October 2006)
J-P,
You have NOTHING to apologize for!! You've been very helpful.
That's EXACTLY what needed to be done!!! I ran visualRect through qDebug() after every page and found that y() was incrementing by 1350. I also found out that after printing 45 rows, option.rect.x() was 1000 and not 0. So, I did painter->translate(0, -1350) after calling printer->newPage() and everything prints beautifully. I can't thank you enough for your help!! I was really appreciate it!![]()
Qt Code:
{ 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, pscale); painter->translate(area.x() + xscale, area.y() + yscale); bool ok; int x=0; QStyleOptionViewItem option; for (int r = 0; r < rows; ++r) { ++x; for (int c = 0; c < columns; ++c) { option = viewOptions(); option.rect = visualRect(idx); itemDelegate()->paint(painter, option, idx); } if (x == 45) { ok = printer->newPage(); x=0; painter->translate(0, -1350); } } }To copy to clipboard, switch view to plain text mode
I don't understand what to do before calling the function and what to set before for the three arguments :
Qt Code:
To copy to clipboard, switch view to plain text mode
Do we call it from the mainwindow?
What to do to print? Where is the line to print our QTableView? As the line used in the post from jpn the 29th September 2006 22:43 with :
Qt Code:
//[...] //[...]To copy to clipboard, switch view to plain text mode
Can you put the entire code on-line please with what to be done before and after the function printTable to print a QTableWidget. I am beginner with Qt and i need some full code example...
Thank you for your help.
Alex
hi friend . how i do translate this part code from c++ to python?
for (int c = 0; c < cols; ++c)
{
QModelIndex idx = model()->index(r, c);
QStyleOptionViewItem option = viewOptions();
option.rect = visualRect(idx);
itemDelegate()->paint(painter, option, idx);
}
Bookmarks