Results 1 to 1 of 1

Thread: Hyperlinks in QTextEdit

  1. #1
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Hyperlinks in QTextEdit

    I have a QTextEdit (although I have also tried it with a QTextBrowser with the same results ) and I can't get hyperlinks to display in it.

    You can check out the code from http://knfoviewer.googlecode.com/svn/trunk/ but the part where it's going wrong is in http://knfoviewer.googlecode.com/svn...iewer_part.cpp :

    Qt Code:
    1. bool KNfoViewerPart::openFile()
    2. {
    3. // m_file is always local so we can use QFile on it
    4. QFile file( m_file );
    5.  
    6. if( !file.open( IO_ReadOnly ) )
    7. return false;
    8.  
    9. QString str;
    10. QTextStream stream( &file );
    11. CP437Codec codec;
    12. stream.setCodec( &codec );
    13. QRegExp exp( "http://*" );
    14.  
    15. while( !stream.atEnd() ){
    16. QString s = stream.readLine();
    17. int pos = 0;
    18.  
    19. while( ( pos = s.find( exp, pos ) ) > -1 ){
    20.  
    21. int end = pos + 7;
    22. QChar c( s.at( end ) );
    23.  
    24. while( !c.isSpace() && c.category() != QChar::Separator_Line && end != s.length() ){
    25. end++;
    26. c = s.at( end );
    27. }
    28.  
    29. QString l = s.mid( pos, end - pos );
    30. QString link( "<a href=\"" + l + "\">" + l + "</a>" );
    31. s.replace( pos, l );
    32. pos += link.length();
    33. }
    34.  
    35. str += s + "<br>";
    36. }
    37.  
    38. file.close();
    39.  
    40. // now that we have the entire file, display it
    41. m_widget->setText( str );
    42.  
    43. // just for fun, set the status bar
    44. emit setStatusBarText( m_url.prettyURL() );
    45.  
    46. return true;
    47. }
    To copy to clipboard, switch view to plain text mode 
    Basically what's happening is that every line is being examined for a URL, and if one is found some formatting is applied. But even though rich text is being forced on they are not being rendered as hyperlinks, even though linksUnderlined() returns true and the tagging is correct.

    Does anyone know why this is happening?

    You can get a sample NFO file to test with here, just strip the .txt extension :
    Attached Files Attached Files
    Last edited by Valheru; 9th December 2007 at 21:49. Reason: reformatted to look better

Similar Threads

  1. Re-implement mouse events of QTextEdit
    By Ankitha Varsha in forum Qt Programming
    Replies: 2
    Last Post: 14th October 2006, 16:55
  2. QTextEdit API questions (plain text)
    By Gaspar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 06:03
  3. [QT 4] QTextEdit performance
    By fellobo in forum Qt Programming
    Replies: 8
    Last Post: 6th March 2006, 19:27
  4. Painting to QTextEdit
    By gesslar in forum Qt Programming
    Replies: 8
    Last Post: 18th February 2006, 18:40
  5. Obtaining clean (x)html from QTextEdit
    By ccf_h in forum Qt Programming
    Replies: 1
    Last Post: 5th February 2006, 14:47

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.