void TextEditorWidget
::printDocument(QPrinter *printer
) {
//Header & Footer .... just building the strings...
printPainter.setFont(f); //<- f: is the font used as standard in the QTextEdit
QRect pageRect
= printer
->pageRect
();
QRect headerRect
= printPainter.
boundingRect(printPainter.
window()/*pageRect*/,Qt
::AlignLeft |Qt
::AlignTop, printHeader
);
QRect footerRect
= printPainter.
boundingRect(printPainter.
window()/*pageRect*/,Qt
::AlignBottom |Qt
::AlignHCenter, footer
);
QRect numbersRect
= printPainter.
boundingRect(printPainter.
window()/*pageRect*/,Qt
::AlignLeft,
QString::number(document
->blockCount
()));
pageRect.setTop(headerRect.height() +17);// adding a spacing
pageRect.setLeft(numbersRect.width() +8);
pageRect.setBottom(footerRect.top() -10);
pageRect.setRight(/*pageRect.left() + printer->pageRect().width()*/ printPainter.window().right());
document->setPageSize(pageRect.size());
lay->setPaintDevice(printer);
int pages = lay->pageCount();
int pageNumber = 0;
int yOffset = 0;
bool newPage = true;
printPainter.setClipping(true); //thought this might have something to do with the porblem, but i couldnt see any difference whether set true or false
while(block.isValid()) //<- Iteration Loop
{
if(newPage)
{
++ pageNumber;
printPainter.
drawText(QRect(QPoint(0,
0),headerRect.
size()),Qt
::AlignLeft, printHeader
);
printPainter.drawText(printPainter.window(),Qt::AlignBottom|Qt::AlignHCenter,
yOffset = pageRect.top();
newPage = false;
}
for (int i=0; i< lT->lineCount(); i++) //Line Iteration usualy 1 line per Block
{
if(i
==0) printPainter.
drawText(QRect(QPoint(0,yOffset
),numbersRect.
size()),Qt
::AlignRight,
QString::number(block.
blockNumber()+1));
lT
->lineAt
(i
).
draw(&printPainter,
QPointF(pageRect.
left(),yOffset
));
}
yOffset += lT->boundingRect().height(); // Increment Horizontal offset by size of last Block
if (yOffset >= printPainter.window().bottom()- (footerRect.height()+17+10))
{
newPage = true;
printer->newPage();
}
block = block.next();
}
}
void TextEditorWidget::printDocument(QPrinter *printer)
{
QTextDocument *document = editor->document();
//Header & Footer .... just building the strings...
QPainter printPainter(printer);
printPainter.setFont(f); //<- f: is the font used as standard in the QTextEdit
QRect pageRect = printer->pageRect();
QRect headerRect = printPainter.boundingRect(printPainter.window()/*pageRect*/,Qt::AlignLeft |Qt::AlignTop, printHeader);
QRect footerRect = printPainter.boundingRect(printPainter.window()/*pageRect*/,Qt::AlignBottom |Qt::AlignHCenter, footer);
QRect numbersRect = printPainter.boundingRect(printPainter.window()/*pageRect*/,Qt::AlignLeft,
QString::number(document->blockCount()));
pageRect.setTop(headerRect.height() +17);// adding a spacing
pageRect.setLeft(numbersRect.width() +8);
pageRect.setBottom(footerRect.top() -10);
pageRect.setRight(/*pageRect.left() + printer->pageRect().width()*/ printPainter.window().right());
document->setPageSize(pageRect.size());
QAbstractTextDocumentLayout *lay = document->documentLayout();
lay->setPaintDevice(printer);
int pages = lay->pageCount();
QTextBlock block = document->begin();
int pageNumber = 0;
int yOffset = 0;
bool newPage = true;
printPainter.setClipping(true); //thought this might have something to do with the porblem, but i couldnt see any difference whether set true or false
while(block.isValid()) //<- Iteration Loop
{
if(newPage)
{
++ pageNumber;
printPainter.drawText(QRect(QPoint(0,0),headerRect.size()),Qt::AlignLeft, printHeader);
printPainter.drawText(printPainter.window(),Qt::AlignBottom|Qt::AlignHCenter,
footer.arg(QString::number(pageNumber),QString::number(pages)));
yOffset = pageRect.top();
newPage = false;
}
QTextLayout *lT = block.layout();
for (int i=0; i< lT->lineCount(); i++) //Line Iteration usualy 1 line per Block
{
if(i==0) printPainter.drawText(QRect(QPoint(0,yOffset),numbersRect.size()),Qt::AlignRight,
QString::number(block.blockNumber()+1));
lT->lineAt(i).draw(&printPainter,QPointF(pageRect.left(),yOffset));
}
yOffset += lT->boundingRect().height(); // Increment Horizontal offset by size of last Block
if (yOffset >= printPainter.window().bottom()- (footerRect.height()+17+10))
{
newPage = true;
printer->newPage();
}
block = block.next();
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks