Hi

I am using below code to draw an html. I need to display text in the item as link, once clicked on a text (not on the item it self) it perform some action. I was using setIndexWidget and QLabel but performance is pretty bad.

An here the question how to make a text clickable ( not the item) to behave like an html link.

Possible ? thanks for help

Qt Code:
  1. QSize HyperLinkDelegate::sizeHint(const QStyleOptionViewItem & option ,
  2. const QModelIndex & index) const
  3. {
  4. QStyleOptionViewItemV4 optionV4 = option;
  5. initStyleOption(&optionV4, index);
  6.  
  7. doc.setHtml(optionV4.text);
  8. doc.setTextWidth(optionV4.rect.width());
  9. return QSize(doc.idealWidth(), doc.size().height());
  10. }
  11.  
  12. void HyperLinkDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
  13. const QModelIndex &index) const
  14. {
  15. QStyleOptionViewItemV4 optionV4 = option;
  16. initStyleOption(&optionV4, index);
  17.  
  18. QStyle *style = optionV4.widget? optionV4.widget->style() : QApplication::style();
  19.  
  20. doc.setHtml(optionV4.text);
  21.  
  22. // Painting item without text
  23. optionV4.text = QString();
  24. style->drawControl(QStyle::CE_ItemViewItem, &optionV4, painter);
  25.  
  26. QAbstractTextDocumentLayout::PaintContext ctx;
  27.  
  28. // Highlighting text if item is selected
  29. if (optionV4.state & QStyle::State_Selected)
  30. ctx.palette.setColor(QPalette::Text, optionV4.palette.color(QPalette::Active, QPalette::HighlightedText));
  31.  
  32. QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, &optionV4);
  33. painter->save();
  34. painter->translate(textRect.topLeft());
  35. painter->setClipRect(textRect.translated(-textRect.topLeft()));
  36. doc.documentLayout()->draw(painter, ctx);
  37. painter->restore();
  38.  
  39. }
To copy to clipboard, switch view to plain text mode