PDA

View Full Version : Hyperlinks in QTextEdit



Valheru
9th December 2007, 21:44
I have a QTextEdit (http://doc.trolltech.com/latest/qtextedit.html) (although I have also tried it with a QTextBrowser (http://doc.trolltech.com/latest/qtextbrowser.html) 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/trunk/src/knfoviewer_part.cpp :


bool KNfoViewerPart::openFile()
{
// m_file is always local so we can use QFile on it
QFile file( m_file );

if( !file.open( IO_ReadOnly ) )
return false;

QString str;
QTextStream stream( &file );
CP437Codec codec;
stream.setCodec( &codec );
QRegExp exp( "http://*" );

while( !stream.atEnd() ){
QString s = stream.readLine();
int pos = 0;

while( ( pos = s.find( exp, pos ) ) > -1 ){

int end = pos + 7;
QChar c( s.at( end ) );

while( !c.isSpace() && c.category() != QChar::Separator_Line && end != s.length() ){
end++;
c = s.at( end );
}

QString l = s.mid( pos, end - pos );
QString link( "<a href=\"" + l + "\">" + l + "</a>" );
s.replace( pos, l );
pos += link.length();
}

str += s + "<br>";
}

file.close();

// now that we have the entire file, display it
m_widget->setText( str );

// just for fun, set the status bar
emit setStatusBarText( m_url.prettyURL() );

return true;
}
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 :