[SOLVED] QPlainTextEdit Print question
-
DUH. I mentally read height instead of y in line 34 in the code below. There is no problem and the code snippet will print out the text and is easily modified to print headers and footers.
I'm a little embarrassed to ask this question but here goes. Some time ago I wrote some code to print out headers and footers for a plain text editor. Now I want to expand this to include line numbers but I forgot why some things in my original code works and I need to rediscover why it works (I understood it when I first wrote it, I think).
One line which has nothing to do with headers or footers in particular stumps me. A simplified code snippet illustrates the problem:
@// current printer supplied as an argument
printDocument=ui->himpPlainTextEdit->document()->clone();
printDocument->documentLayout()->setPaintDevice(currentPrinter);currentPrinter->setPageMargins(marginLeftF,marginTopF,marginRightF,marginBottomF,marginUnit);
QPainter painter(currentPrinter);
// set the page drawing area (smaller than the entire page area because of margins)
QRect drawingArea(currentPrinter->pageRect());// find the content drawing area size for one page
QSizeF pageContentSize(drawingArea.width(),drawingArea.height());// set the document page size
printDocument->setPageSize(pageContentSize);// set up the bounding rectangles
QRect contentRect=QRect(QPoint(0,0),printDocument->size().toSize());// translate the painter orgin to the drawing area
painter.translate(marginLeftF,marginTopF);// initialize the loop rectangle
QRect currentRect=QRect(QPoint(0,0),pageContentSize.toSize());
painter.save();// loop until currentRect no longer intersects with contentRect
while(currentRect.intersects(contentRect))
{
// restore/save the painter and draw the current contents page
painter.restore();
painter.save();
painter.translate(0,-currentRect.y()); // ???
printDocument->drawContents(&painter,currentRect);// adjust y printer coordinate to print the next page by translating currentRect to the next page currentRect.translate(0,currentRect.height()); // advance to a new page if there is anything left to print if(currentRect.intersects(contentRect))currentPrinter->newPage();
}// end currentRect while loop
painter.restore();
painter.end();
@The conceptual problem I have is line 34 in the above code.
@ painter.translate(0,-currentRect.y()); // ??? @Looking at other code snippets such as:
"http://qt-project.org/faq/answer/is_it_possible_to_set_a_header_and_footer_when_printing_a_qtextdocument":http://qt-project.org/faq/answer/is_it_possible_to_set_a_header_and_footer_when_printing_a_qtextdocument
it appears that this line is unnecessary.Indeed if eliminate this line of code then the first page of the document is printed correctly. Unfortunately all subsequent pages are not printed correctly. If I include this line of code then every page is printed correctly.
My question is why is this line of code necessary? In fact how does the first page get printed correctly when this line of code is included?
Any help will be appreciated (FYI: I'm using 4.8 on windows 7).