Results 1 to 3 of 3

Thread: QtextDocument-ItemDelegate and Painting

  1. #1
    Join Date
    Nov 2009
    Posts
    44
    Thanks
    8
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QtextDocument-ItemDelegate and Painting

    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:
    Qt Code:
    1. q_txt.setDefaultFont( iter.Data()->GetColumnFont());
    2. q_txt.setTextWidth( (float)iter.Data()->GetWidth() );
    3. q_txt.setDocumentMargin( 0 );
    4. //set row height equal to the tallest text in the line
    5. QString col_text( (char*)iter.Data()->GetColumnText() );
    6. if( col_text.contains( "<!DOCTYPE html" ) )
    7. {
    8. q_txt.setHtml( col_text );
    9. }
    10. else
    11. q_txt.setPlainText( col_text );
    12.  
    13. if( height < q_txt.size().height() )
    14. height = q_txt.size().height();
    To copy to clipboard, switch view to plain text mode 

    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:
    Qt Code:
    1. q_txt.setDocumentMargin( 0 );
    2. q_txt.setHtml( col_text );
    3. q_txt.setTextWidth( iter.Data()->GetWidth() );
    4. //painter->setViewport( r.x(), r.y(), r.width(), r.height());
    5. painter->translate( r.x(),r.y());
    6. q_txt.drawContents( painter );
    7. // QAbstractTextDocumentLayout::PaintContext context;
    8. // q_txt.setPageSize( arect.size());
    9. // painter->translate( arect.x(),arect.y());
    10. // q_txt.documentLayout()->draw(painter, context);
    11.  
    12. painter->restore();
    13. // painter->drawRect( r );
    To copy to clipboard, switch view to plain text mode 
    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!

  2. The following user says thank you to LynneV for this useful post:

    kornerr (14th November 2010)

  3. #2
    Join Date
    Jun 2008
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QtextDocument-ItemDelegate and Painting

    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
    Qt4 lover

  4. #3
    Join Date
    Nov 2009
    Posts
    44
    Thanks
    8
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QtextDocument-ItemDelegate and Painting

    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.
    Qt Code:
    1. painter->save();
    2. q_txt.setHtml( theHTMLText );
    3. q_txt.setTextWidth( theTextWidth );
    4. painter->translate( rect.x(), rect.y() );
    5. q_txt.drawContents( painter );
    6. painter->restore();
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Display ItemDelegate Editor always
    By gast23 in forum Qt Programming
    Replies: 6
    Last Post: 6th September 2010, 04:33
  2. QTreeView and itemDelegate code
    By alexandernst in forum Newbie
    Replies: 1
    Last Post: 22nd April 2010, 17:00
  3. Can I use a QWidget as a renderer in an ItemDelegate?
    By boblebel in forum Qt Programming
    Replies: 2
    Last Post: 2nd May 2009, 20:03
  4. Color table row with ItemDelegate
    By dexjam in forum Newbie
    Replies: 8
    Last Post: 29th June 2006, 12:56
  5. Replies: 0
    Last Post: 28th June 2006, 20:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.