Hi

I'm using a QPlainTextEdit for text chat and would like to highlight URLs. I do this by subclassing QSyntaxHighlighter and call setFormat() like this:

Qt Code:
  1. class UrlSyntaxHighlighter : public QSyntaxHighlighter
  2. ...
  3. void highlightBlock(const QString& text)
  4. {
  5. QString pattern = "(http://[^ ]+)";
  6. QRegExp expression(pattern);
  7. int index = text.indexOf(expression);
  8. while (index >= 0) {
  9. int length = expression.matchedLength();
  10. QTextCharFormat myClassFormat = format(index);
  11. myClassFormat.setFontUnderline(true);
  12. myClassFormat.setForeground(Qt::blue);
  13. myClassFormat.setAnchor(true);
  14. myClassFormat.setAnchorHref(expression.cap(1));
  15. myClassFormat.setAnchorName(expression.cap(1));
  16. setFormat(index, length, myClassFormat);
  17. index = text.indexOf(expression, index + length);
  18. }
  19. }
To copy to clipboard, switch view to plain text mode 

In my QPlainTextEdit I'm then overriding mouseMoveEvent(.) to change the mouse cursor when the anchor is hovered. However, when I access the QTextCursor using QPlainTextEdit::cursorForPosition(.) and extract the QTextCharFormat there is no anchor set on the text. Anyone know why?

Btw, I know I can use QTextEdit instead which has better support for anchors but I try and keep memory low and I don't need all the fancy picture-stuff. Also I'm aware that the regex isn't entirely correct.

-- Bjoern