Results 1 to 3 of 3

Thread: QPlainTextEdit and anchors

  1. #1
    Join Date
    Sep 2006
    Posts
    19
    Thanks
    12

    Default QPlainTextEdit and anchors

    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

  2. #2
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPlainTextEdit and anchors

    does the highlighting even show up? did you enable mouse tracking? could you also post the code of your mouseMoveEvent?
    Current Qt projects : QCodeEdit, RotiDeCode

  3. #3
    Join Date
    Sep 2006
    Posts
    19
    Thanks
    12

    Default Re: QPlainTextEdit and anchors

    Hi

    I've solved it in a rather cumbersome way where I use cursorForPosition() to manually sweep each QTextBlock for a URL.

    Here is basically what I do:

    Qt Code:
    1. QString ChatTextEdit::currentUrl(QMouseEvent* e) const
    2. {
    3. QTextDocument* doc = document();
    4.  
    5. int cursor_pos = cursorForPosition(e->pos()).position();
    6. QTextBlock block = doc->findBlock(cursor_pos);
    7. int block_pos = block.position();
    8.  
    9. QString text = block.text();
    10.  
    11. QVector<int> url_index, url_length;
    12.  
    13. int index = 0;
    14. QString url;
    15. do
    16. {
    17. int length = 0;
    18. url = urlFound(text, index, length);
    19. if(url.size())
    20. {
    21. url_index.push_back(index);
    22. url_length.push_back(length);
    23. urls.push_back(url);
    24. }
    25. index += length;
    26. }
    27. while(url.size());
    28.  
    29. url.clear();
    30.  
    31. for(int i=0;i<url_index.size();i++)
    32. {
    33. if(cursor_pos >= block_pos+url_index[i] &&
    34. cursor_pos < block_pos+url_index[i]+url_length[i])
    35. {
    36. url = urls[i];
    37. break;
    38. }
    39. }
    40.  
    41. return url;
    42. }
    To copy to clipboard, switch view to plain text mode 

    -- Bjoern

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.