Results 1 to 20 of 20

Thread: rich text in tree widget

  1. #1
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default rich text in tree widget

    What's the easiest way to display rich text in QTreeWidget or QTreeView items? If it involves implementing QTreeView::drawRow(), I'd be grateful for some help getting started ... is there an example I can copy?

    Thanks!

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: rich text in tree widget

    Try something like what's in this post.
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    magland (26th October 2007)

  4. #3
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: rich text in tree widget

    Thanks, do you recommend I inherit QItemDelegate, or QAbstractItemDelegate?

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: rich text in tree widget

    Depends. If you don't want to display icons or anything (like selections), use QAbstractItemDelegate. Otherwise use QItemDelegate, just make sure you reimplement sizeHint() as well.

  6. #5
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: rich text in tree widget

    Quote Originally Posted by wysota View Post
    Depends. If you don't want to display icons or anything (like selections), use QAbstractItemDelegate. Otherwise use QItemDelegate, just make sure you reimplement sizeHint() as well.
    I want to use all features of QItemDelegate, except of course rendering text. So I inherited from that and overloaded as below. That works quite well. But my only problem now is that painter->translate(...) does not seem to be correct... it's off by about one line... any ideas?

    Qt Code:
    1. void drawDisplay(QPainter *painter, const QStyleOptionViewItem &option,const QRect &rect, const QString &text) const {
    2. painter->save();
    3. QTextEdit edit;
    4. QTextDocument *doc=edit.document();
    5. if (highlighter) highlighter->setDocument(doc);
    6. edit.setLineWrapMode(QTextEdit::NoWrap);
    7.  
    8. doc->setPlainText(text);
    9. QAbstractTextDocumentLayout::PaintContext context;
    10. doc->setPageSize( rect.size());
    11. painter->translate(rect.x(),rect.y());
    12. doc->documentLayout()->draw(painter, context);
    13. painter->restore();
    14. }
    To copy to clipboard, switch view to plain text mode 

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: rich text in tree widget

    Remember that the size of the rectangle is off by one because of "historical reasons". You might be hitting the offset here. Try reducing the width and height of rect by one.

  8. The following user says thank you to wysota for this useful post:

    magland (27th October 2007)

  9. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: rich text in tree widget

    Also, to me it doesn't look like a very good idea to create a QTextEdit widget during every drawDisplay(). Wouldn't a QTextDocument suffice?
    J-P Nurmi

  10. #8
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: rich text in tree widget

    Quote Originally Posted by jpn View Post
    Also, to me it doesn't look like a very good idea to create a QTextEdit widget during every drawDisplay(). Wouldn't a QTextDocument suffice?
    I agree, but I couldn't figure out how to do QTextEdit::setLineWrapMode(QTextEdit::NoLineWrap) with QTextDocument. Do you know how to do that?

    Also, I guess I should allocate a single QTextEdit and QTextDocument for the entire tree widget... and just keep calling setPlainText from within drawDisplay. Would that make more sense?

  11. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: rich text in tree widget

    Quote Originally Posted by magland View Post
    I agree, but I couldn't figure out how to do QTextEdit::setLineWrapMode(QTextEdit::NoLineWrap) with QTextDocument. Do you know how to do that?
    No idea, but I'd take a look at QTextEdit sources and see how Trolls do it. I bet they do it with the help of QTextDocument or its document layout.

    Also, I guess I should allocate a single QTextEdit and QTextDocument for the entire tree widget... and just keep calling setPlainText from within drawDisplay. Would that make more sense?
    Try profiling and see if there's any difference. Changing the document content causes a lot of re-layouting as well. I don't know which one is more light-weight; to create a new document from scratch or to re-layout new document content.
    J-P Nurmi

  12. The following user says thank you to jpn for this useful post:

    magland (29th October 2007)

  13. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: rich text in tree widget

    Trolls do everything with QTextControl, so I'm guessing the functionality is performed by this exact component. If I'm wrong, then it's probably in the competence of the text layout object - try there.

  14. #11
    Join Date
    Aug 2007
    Posts
    166
    Thanks
    16
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: rich text in tree widget

    Hi,

    I followed this thread instructions and I created delegate to paint rich text and also to not draw the forus of items. I inherit QItemDelegate. Here's my code:

    Qt Code:
    1. void ItemDelegate::drawDisplay( QPainter* painter, const QStyleOptionViewItem& option, const QRect& rect, const QString& text ) const
    2. {
    3. QPen pen = painter->pen();
    4. QPalette::ColorGroup cg = option.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;
    5.  
    6. if( cg == QPalette::Normal && !(option.state & QStyle::State_Active) )
    7. cg = QPalette::Inactive;
    8.  
    9. if( option.state & QStyle::State_Selected )
    10. {
    11. painter->fillRect( rect, option.palette.brush(cg, QPalette::Highlight) );
    12. painter->setPen( option.palette.color(cg, QPalette::HighlightedText) );
    13. }
    14. else
    15. {
    16. painter->setPen( option.palette.color(cg, QPalette::Text) );
    17. }
    18.  
    19. if( text.isEmpty() )
    20. return;
    21.  
    22. painter->save();
    23. doc.setHtml( text );
    24. doc.adjustSize();
    25. QAbstractTextDocumentLayout::PaintContext context;
    26.  
    27. doc.setPageSize( rect.size() );
    28.  
    29. painter->setClipRect( rect );
    30. painter->translate( rect.x(), rect.y() );
    31. doc.documentLayout()->draw( painter, context );
    32. painter->restore();
    33. }
    34.  
    35. void ItemDelegate::drawFocus( QPainter* painter, const QStyleOptionViewItem& option, const QRect& rect ) const
    36. {
    37. }
    To copy to clipboard, switch view to plain text mode 

    The code is working fine but I am no longer able to use style sheets on the items. In order to use style sheets I think that I have to inherit QStyledItemDelegate. But QStyledItemDelegate doesn't have virtual functions drawDisplay() and drawFocus(). So how I will be able now to remove the focus dotted lines of items and to draw the rich text and I really want to avoid to rewrite the whole paint() function... Please help.

  15. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: rich text in tree widget

    If you want to use QStyledItemDelegate, you have to reimplement those methods that it delivers, unfortunately. Focus is kept inside the style option object, you can copy it and make sure the focus flag is off before calling the base class implementation.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. The following user says thank you to wysota for this useful post:

    The Storm (11th March 2009)

  17. #13
    Join Date
    Aug 2007
    Posts
    166
    Thanks
    16
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: rich text in tree widget

    Thank you a lot. For me (a person who is not in good standing with math) it was a bit hard to find the correct rect on where I should place the text, sinse I put a Icon on the item too. The good in QItemDelegate was that it gived me as argument the exact rect where a text can be placed. In QStyledItemDelegate I wasn't able to find a correct way to calculate this so now I use hardcoded rect. If you have an idea how I can calculate the correct rect for the text, please tell me.

  18. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: rich text in tree widget

    You have the sources of Qt, take a look at the appropriate method.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  19. #15
    Join Date
    Aug 2007
    Posts
    166
    Thanks
    16
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: rich text in tree widget

    I lost half a day trying to figure it out via the Qt sources. The bad part is that there is thoose private pointer d that make the things hard for me. When I have more time I will try to cut out the peaces of code that combined can give me correct result.

  20. #16
    Join Date
    Aug 2007
    Posts
    166
    Thanks
    16
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: rich text in tree widget

    Ok just one more question. The selected item is highlighted as it should be but the icon that I have set to it receives a blue highlight too. What variable I have to modify in to the option pointer in order to make the icon/picture to remain in normal state?

    I tried with
    option->showDecorationSelected = false;
    before the paint and the whole highlightion dissappeared except that on the picture. This makes me think that there should be some kind of variable to just leave the picture in normal state but I can't find it...

  21. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: rich text in tree widget

    You should paint the icon again after everything else is drawn. That's the only solution I can think of.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  22. #18
    Join Date
    Aug 2007
    Posts
    166
    Thanks
    16
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: rich text in tree widget

    I was afraid that you will say that. Not good solution but sinse it seems to be the only way I will do it.

  23. #19
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: rich text in tree widget

    You can try modifying QStyleOption::state before calling the drawDecoration function.

    Qt Code:
    1. QStyle::State state = option.state;
    2. state = state & (!QStyle::State_Selected);
    To copy to clipboard, switch view to plain text mode 
    something like that...

  24. #20
    Join Date
    Aug 2007
    Posts
    166
    Thanks
    16
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: rich text in tree widget

    Yep I tried removing the QStyle::State_Selected flag but this removes the whole highlight not only on the picture, i.e. the user don't know witch is the active item.

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. ‎a lightweight text showing widget
    By jacum in forum Qt Programming
    Replies: 1
    Last Post: 22nd August 2007, 14:44
  3. Editable text in QGraphicsView
    By wysota in forum Qt Programming
    Replies: 8
    Last Post: 24th February 2007, 15:30
  4. Rich Text Format
    By Kapil in forum Newbie
    Replies: 1
    Last Post: 4th October 2006, 10:53
  5. widget for text AND buttons
    By nongentesimus in forum Newbie
    Replies: 2
    Last Post: 16th June 2006, 13:43

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.