PDA

View Full Version : Printing with QTextEdit



vermarajeev
14th November 2007, 12:12
Hello guys,
Is there any problem with the following code snippet?
The contents of the QTextEdit are printed on paper properly but after the printing
is done, the QTextEdit disappears completely from the canvas i.e the contents on the
canvas disappears. Is there anything I'm missing in the below code?

I suppose once the layout of the QTextEdit is changed I need to reset it back to
original form. But no luck. Can someone please help me with this.


void myClass::printEditorContents( QRect headerRect, QPainter &paint )
{
QAbstractTextDocumentLayout *layout = _textEdit->document()->documentLayout();
_textEdit->document()->setDefaultFont(defaultFont);

paint.save();

paint.setFont(defaultFont);
QPaintDevice *pd = layout->paintDevice();
layout->setPaintDevice(paint.device());

QTextFrameFormat fmt = _textEdit->document()->rootFrame()->frameFormat();
fmt.setMargin(margin.x());
fmt.setHeight(headerRect.height());
fmt.setWidth(headerRect.width());

_textEdit->document()->rootFrame()->setFrameFormat(fmt);
_textEdit->document()->drawContents(&paint);
layout->setPaintDevice(pd);
paint.restore();
}

Thanks in advance.

vermarajeev
14th November 2007, 12:45
I hope I'm clear if not please let me....

wysota
14th November 2007, 12:47
Why don't you use QTextDocument::print()?