Results 1 to 6 of 6

Thread: QStandardItem : subpart of the text as bold

  1. #1
    Join Date
    Feb 2007
    Posts
    12

    Default QStandardItem : subpart of the text as bold

    how to bold a part of the text of a QStandardItem ?
    atm i see that i can choose the font of the whole text, is this possible to change only a part of the font of the text ?

  2. #2
    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: QStandardItem : subpart of the text as bold

    I take it that you ask how to bold a fragment of data in the view... The answer is to provide a custom delegate that can handle rich text.

  3. #3
    Join Date
    Feb 2007
    Posts
    12

    Default Re: QStandardItem : subpart of the text as bold

    i then need to create a qtextedit when i enter in QItemDelegate:: paint(...) ?
    Last edited by titoo; 9th February 2007 at 10:40.

  4. #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: QStandardItem : subpart of the text as bold

    No. Use QTextLayout or something simmilar... I did it like this:

    Qt Code:
    1. void CommentDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
    2. {
    3. painter->save();
    4. doc.setHtml( index.data().toString() );
    5. QAbstractTextDocumentLayout::PaintContext context;
    6. doc.setPageSize( option.rect.size());
    7. painter->translate(option.rect.x(), option.rect.y());
    8. doc.documentLayout()->draw(painter, context);
    9. painter->restore();
    10. }
    To copy to clipboard, switch view to plain text mode 

  5. The following 4 users say thank you to wysota for this useful post:

    Darktib (6th February 2011), lamera (13th December 2008), tpf80 (16th October 2009), wojtek (21st February 2008)

  6. #5
    Join Date
    Feb 2007
    Posts
    7
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QStandardItem : subpart of the text as bold

    Need to add a line:
    Qt Code:
    1. painter->setClipRect(option.rect);
    To copy to clipboard, switch view to plain text mode 
    before painter->translate(...)
    to avoid drawing text lines outside of the cell.
    And highlight a focus cell changing background colour of text or with fillRect():
    Qt Code:
    1. if (option.state & QStyle::State_Selected)
    2. painter->fillRect(option.rect, option.palette.highlight());
    To copy to clipboard, switch view to plain text mode 
    And change colour of highlighted text.
    For example:
    Qt Code:
    1. void MyDelegate::paint (
    2. QPainter *painter,
    3. const QStyleOptionViewItem& option,
    4. const QModelIndex& index ) const {
    5.  
    6. QString text;
    7. QRect rect;
    8. QVariant value;
    9. QStyleOptionViewItemV4 opt = setOptions(index, option); // or V2 for Qt4.2
    10.  
    11. //value = index.data(Qt::DisplayRole);
    12. //text = value.toString();
    13. //opt.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
    14. //drawDisplay(painter, opt, opt.rect, text);
    15.  
    16. // Change line style around of the cell:
    17. QPen pen = QPen(Qt::red, 1, Qt::DashLine,
    18. Qt::FlatCap, Qt::MiterJoin);
    19. painter->setPen(pen);
    20. painter->drawLine(opt.rect.left(), opt.rect.bottom()+1,
    21. opt.rect.right(), opt.rect.bottom()+1);
    22. painter->drawLine(opt.rect.right()+1, opt.rect.top(),
    23. opt.rect.right()+1, opt.rect.bottom());
    24.  
    25. // draw rich text:
    26. painter->save();
    27. doc.setHtml( index.data().toString() );
    28. QAbstractTextDocumentLayout::PaintContext context;
    29. doc.setPageSize( option.rect.size());
    30. painter->setClipRect(option.rect);
    31. painter->translate(option.rect.x(), option.rect.y());
    32.  
    33. //if (option.state & QStyle::State_Selected)
    34. //painter->setBrush(option.palette.highlightedText());
    35.  
    36. doc.documentLayout()->draw(painter, context);
    37. //doc.drawContents(painter, option.rect);
    38. painter->restore();
    39.  
    40. // draw focus:
    41. if (option.state & QStyle::State_Selected){
    42. QPalette pal=option.palette;
    43. QBrush brush = pal.highlight();
    44. QColor col = brush.color();
    45. col.setAlpha(127);
    46. brush.setColor(col);
    47. painter->fillRect(option.rect, brush);
    48. //painter->fillRect(option.rect, option.palette.highlight());
    49. }
    50. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images
    Last edited by Georgest; 5th October 2008 at 09:43.

  7. The following 3 users say thank you to Georgest for this useful post:

    Darktib (6th February 2011), Momergil (25th August 2014), tpf80 (16th October 2009)

  8. #6
    Join Date
    Oct 2011
    Posts
    9
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QStandardItem : subpart of the text as bold

    To QTableWidget, I derive paint() function of QItemDelegate like this:

    Qt Code:
    1. void TableWidgetItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
    2. const QModelIndex &index) const
    3. {
    4. painter->save();
    5.  
    6. QFont font = qvariant_cast<QFont>(index.data(Qt::FontRole));
    7. painter->setFont(font);
    8. painter->drawStaticText(option.rect.x(),option.rect.y(), QStaticText(index.data().toString()));
    9.  
    10. painter->restore();
    11. }
    To copy to clipboard, switch view to plain text mode 

    _ Draw rich text with Qt::AlignCenter:

    Qt Code:
    1. void TableWidgetItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
    2. const QModelIndex &index) const
    3. {
    4. QFont font = qvariant_cast<QFont>(index.data(Qt::FontRole));
    5. QBrush brush = qvariant_cast<QBrush>(index.data(Qt::ForegroundRole));
    6.  
    7. QFontMetrics fm(font);
    8. QRect boundingRect = fm.boundingRect(index.data().toString().remove("<sub>").remove("</sub>")); //We can remove other html tags
    9. int stringWidth = boundingRect.width();
    10. int stringHeight = boundingRect.height();
    11.  
    12. int xPoint = option.rect.x() + option.rect.width()/2 - stringWidth/2;
    13. int yPoint = option.rect.y() + option.rect.height()/2 - stringHeight/2;
    14.  
    15. painter->save();
    16.  
    17. painter->setFont(font);
    18. painter->setPen(brush.color());
    19. painter->drawStaticText(xPoint , yPoint, QStaticText(index.data().toString()));
    20. }
    To copy to clipboard, switch view to plain text mode 
    Regards.
    Last edited by huyhoangfool; 1st November 2012 at 10:11.

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Problem pasting text into a QTextEdit
    By Spockmeat in forum Qt Programming
    Replies: 8
    Last Post: 14th March 2009, 14:36
  3. visible text of textedit
    By regix in forum Qt Programming
    Replies: 3
    Last Post: 26th June 2006, 09:02

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.