How to make item view render rich (html) text in Qt
2013-11-15 09:17
417 查看
class HtmlDelegate : public QStyledItemDelegate { protected: void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const; QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const; }; void HtmlDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { QStyleOptionViewItemV4 optionV4 = option; initStyleOption(&optionV4, index); QStyle *style = optionV4.widget? optionV4.widget->style() : QApplication::style(); QTextDocument doc; doc.setHtml(optionV4.text); /// Painting item without text optionV4.text = QString(); style->drawControl(QStyle::CE_ItemViewItem, &optionV4, painter); QAbstractTextDocumentLayout::PaintContext ctx; // Highlighting text if item is selected if (optionV4.state & QStyle::State_Selected) ctx.palette.setColor(QPalette::Text, optionV4.palette.color(QPalette::Active, QPalette::HighlightedText)); QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, &optionV4); painter->save(); painter->translate(textRect.topLeft()); painter->setClipRect(textRect.translated(-textRect.topLeft())); doc.documentLayout()->draw(painter, ctx); painter->restore(); }
相关文章推荐
- JS. How to replace html element with another element/text, represented in string?
- How to get html text in a Flex Alert panel.
- Units Problem: How to read text size as custom attr from xml and set it to TextView in java code
- Units Problem: How to read text size as custom attr from xml and set it to TextView in java code
- How to make text of an html checkbox clickable (like ASP.NET control)
- How to enter text into a textfield in a web view using UIAutomation
- How to modify non-customizable entity in MS Dynamics CRM 4.0 (Add Product price column to Price List Item view)
- How to invoke the method of managed bean and render view in JSF when we are outside the lifecycle of JSF
- How to Make Custom Drawn Gradient Backgrounds in a Grouped UITableView with Core Graphics
- How to Make Custom Drawn Gradient Backgrounds in a Grouped UITableView with Core Graphics
- How to change the Text color of Menu item in Android?
- How To Make a Custom UIView in iOS 5: A 5 Star Rating View[转]
- How to display fields in the frontend item view
- How to render "asp:Label" as "label" in HTML markup.
- UIView Tutorial for iOS: How To Make a Custom UIView in iOS 5: A 5 Star Rating View
- Units Problem: How to read text size as custom attr from xml and set it to TextView in java code
- How to put a space character before option text in a HTML select element?
- How to make a sprite from URL in cocos2d-x
- How to make our assemblies appeared in Add Reference dialog box
- How to disable google translate from html in chrome