PDA

View Full Version : QtextDocument-ItemDelegate and Painting



LynneV
13th November 2010, 16:02
Hello Forum! I am having trouble painting a QTextDocument inside a StyledItemDelegate. I am painting all delegates with paint.drawText EXCEPT delegates with rich text contents. Those I am painting with QTextDocument. I am using the same process for printing. In both cases I get the text properly painted but it is putting a blank area before the next delegate paint event. The problem seems to be paint.translate. If I use it, I get properly printed text, but a blank area. If I don't use it, I get no text painted, but the size of the blank area is correct. If I paint a rectangle using painter.drawRect(), it draws a rectangle under the rich text around where the text SHOULD go, and it's the correct size. It's like the painter goes off into some alternate reality, and it doesn't seem to matter how I access the drawing function. Here is the process I am using and code:
Set Size Hint using:


q_txt.setDefaultFont( iter.Data()->GetColumnFont());
q_txt.setTextWidth( (float)iter.Data()->GetWidth() );
q_txt.setDocumentMargin( 0 );
//set row height equal to the tallest text in the line
QString col_text( (char*)iter.Data()->GetColumnText() );
if( col_text.contains( "<!DOCTYPE html" ) )
{
q_txt.setHtml( col_text );
}
else
q_txt.setPlainText( col_text );

if( height < q_txt.size().height() )
height = q_txt.size().height();


And then I try to paint it in the delegate's paint event with (I left in some of my attempts commented out) this code:


q_txt.setDocumentMargin( 0 );
q_txt.setHtml( col_text );
q_txt.setTextWidth( iter.Data()->GetWidth() );
//painter->setViewport( r.x(), r.y(), r.width(), r.height());
painter->translate( r.x(),r.y());
q_txt.drawContents( painter );
// QAbstractTextDocumentLayout::PaintContext context;
// q_txt.setPageSize( arect.size());
// painter->translate( arect.x(),arect.y());
// q_txt.documentLayout()->draw(painter, context);

painter->restore();
// painter->drawRect( r );

Yes, I know this is something stupid and I'm too dense to 'get it', but if someone has the answer, it would make my day!! Thanks in advance!

kornerr
14th November 2010, 09:13
I don't know how to solve your problem, but you did solve mine :)
I had a hard time trying to figure out why my QTextDocument wasn't painting anything to the rectangle I specified at drawContents. Thanks to your code snippet I found out this is only a clipping rectangle and QTextDocument draws at 0, 0 if not translated to other position.
Thank you :P

LynneV
15th November 2010, 17:10
I have the answer!! Basically, I misinterpreted the painter->restore() function. When the documentation says "pops a save state off the stack", it means a save state made with painter->save(), not the last alteration to the painter. So, here is the code that works in paint on a screen and also to a printer. Big points: you have to translate the x and y start point with painter->translate( rect.x(), rect.y() ) where rect is from the option passed to the painter in the ItemDelegate and set by SizeHint. And you have to save the painter and then restore it. If you don't translate, the html text is painted at 0,0. And if you dont save/restore, the text is inserted above the delegate paint rect, and that rect is blank.


painter->save();
q_txt.setHtml( theHTMLText );
q_txt.setTextWidth( theTextWidth );
painter->translate( rect.x(), rect.y() );
q_txt.drawContents( painter );
painter->restore();