PDA

View Full Version : Edit QTextDocument for printing



ericV
29th July 2009, 13:45
As I am working on a custom Code Editor I have run into a new problem. :cool:

I want to be able to print the QTextDocument, but prepend the line number in each block.

Printing the document as is is no problem, thanks to
QTextDocument::print()

I was thinking along the lines of cloning the original document and editing each block... but i cant find any way to insert additional text into it.

and i don't want to get the plain text out, because it would destroy the formatting created by my highlighter... ( This would also be the reason why I'm looking to do this directly in the document)

Does anyone have an idea?

UPDATE:
Im going to try using a QTextCursor on the document.... i hope this works.

Last Update:

I got it to work (not very elegant though)


void EditorWindow::printDocument(QPrinter &printer)
{
QTextDocument *document = codeEditor->getDocument();
QTextCursor *documentCursor = new QTextCursor(document);

QTextCharFormat numberFormat;
numberFormat.setForeground(QBrush(QColor("blue")));
numberFormat.setFontItalic(true);
documentCursor->beginEditBlock();
do{
documentCursor->insertText("<"+QString::number(documentCursor->blockNumber()+1,10)
+">\t",numberFormat
);

}while(documentCursor->movePosition(QTextCursor::NextBlock));

documentCursor->endEditBlock();
document->print(&printer);
document->undo();

}