PDA

View Full Version : Printing more than one page



KillGabio
26th October 2014, 05:36
Hi everyone, I`m having some trouble trying to understand how to print several pages when i have many rows. I tried to follow this (http://blog.qt.digia.com/blog/2012/08/24/qt-commercial-support-weekly-25-printing-large-tables-2/) example but all code is not available.

I managed to get the following solution (my idea is to print two pages top):


if (rows>32){
double pageHeight=tempTable.horizontalHeader()->height()+ 32*tempTable.rowHeight(1);
QPixmap grabPixmap = QPixmap::grabWidget(&tempTable);
QRectF sourceRect;
QPainter painter;
painter.begin(&printer);
sourceRect.setWidth(printer.pageRect().width());
sourceRect.setHeight(pageHeight);
painter.drawPixmap(printer.pageRect().topLeft(), grabPixmap,sourceRect);
printer.newPage();
painter.translate(0, -1000); //with this line i manage to show some part of the table, if i dont have this line the first page is written over the
//seccond page
sourceRect.setTop(sourceRect.top() + pageHeight);
painter.drawPixmap(printer.pageRect().topLeft(), grabPixmap,sourceRect);
tempTable.render(&painter);
}else tempTable.render(&printer);


result: 10702

I dont understand how to clear the painter so that it paints only from the grabPixmap specified region and then clears to paint the other region for the seccond page without using translate...

Thank you all in advance hope you can understand what i am trying to do.

wysota
26th October 2014, 10:48
I don't understand why you are doing the translate. After calling newPage() the painter is already positioned on the new page and you don't need to do any translation, just paint what you want to get painted. Of course you can't just paint the whole pixmap again as it will paint it from top to bottom. Paint only this part of the pixmap that is to be painted on the second page.

KillGabio
26th October 2014, 19:38
If I dont do the translate I get the following output:

10703

Although I change the sourceRect with this line:
sourceRect.setTop(sourceRect.top() + pageHeight);

It seems to paint the whole first page again over the seccond page (you can actually see the borders of the seccond page table).

Thank you.

wysota
26th October 2014, 19:58
It seems to paint the whole first page again over the seccond page (you can actually see the borders of the seccond page table).

You should calculate how much of the pixmap fits on the first page, paint that there and then calculate how much fits on the next page, paint that there and so on.

Here is an example code that does that (more or less), you can see that it works properly (you can print to PDF):


#include <QApplication>
#include <QtGui>
#include <QPrinter>
#include <QPrintDialog>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setQuitOnLastWindowClosed(false);
QPixmap px(400, 10000);
px.fill(Qt::transparent);
QPainter p(&px);
QLinearGradient grad(0, 0, 400, 10000);
grad.setColorAt(0, Qt::yellow);
grad.setColorAt(1, Qt::red);
p.setPen(Qt::NoPen);
p.setBrush(QBrush(grad));
p.drawRect(px.rect());
p.end();
QPrinter printer;
QPrintDialog dialog(&printer);
if(!dialog.exec()) return 0;
p.begin(&printer);
int offset = 0;
while(offset < px.height()) {
QRect source(QPoint(0, offset), printer.pageRect().size());
p.drawPixmap(printer.pageRect(), px, source);
offset += printer.pageRect().height();
printer.newPage();
}
p.end();
return 0;
}

KillGabio
26th October 2014, 23:43
Thank you very much..you helped me understand everything. ;)

KillGabio
27th October 2014, 01:52
Sorry to bother again. I dont know why, when painting it doesnt respect the setted values, instead it enlarges the pixmap to fit the end of the page no matter how many rows i want to print.

Margins are setted:



int myPageHeight=tempTable.horizontalHeader()->height()+ 32*tempTable.rowHeight(1);
QPixmap grabPixmap = QPixmap::grabWidget(&tempTable);
QPainter painter;
QPrintDialog dialog(&printer);
if(!dialog.exec()) qDebug()<<"fail";
painter.begin(&printer);
QRect sourceRect;
sourceRect.setWidth(printer.pageRect().width());
sourceRect.setHeight(myPageHeight);
int offset = 0;
while(offset < grabPixmap.height()) {
QRect source(QPoint(0, offset), sourceRect.size());
painter.drawPixmap(printer.pageRect(), grabPixmap, source);
offset += myPageHeight;
printer.newPage();
}
painter.end();
qDebug()<< printer.pageRect().height()<< printer.paperRect().height()<<myPageHeight;
//output: 1018 1056 981


for example when printing with a height of 25 rows i get the following output:

10704

wysota
27th October 2014, 08:49
I think you just need to experiment with your calculations. The code I gave you was not a complete solution but rather a rough approach to show the general idea how to print things spanning on multiple pages.

KillGabio
27th October 2014, 12:54
I did play with different heights and rects, I dont know why when drawing the pixmap, it respects left and top margins but expands everything to the bottom of the page no matter of the height i calculate.

I managed to make it work selecting a smaller rectangle, but i dont know why if my pageRect is smaller than the paperRect when I print it expands.

Sorry to bother.

d_stranz
27th October 2014, 22:11
Check your *printer* settings (not in Qt, in the print dialog). Some printers have a "scale to fit" setting that will enlarge an image to fit the full page, regardless of the size of the input image. This could be what it happening in your case.

KillGabio
28th October 2014, 00:26
Thanks for your reply but I already checked that. My solution was to create the rect source same size as my defined sourceRect. Still cant figure out why previous implementation expanded, but need to move on lol. Dont waste any more time people.