PDA

View Full Version : Display '<' as HTML in QString



mclark
6th July 2007, 19:58
Greetings,

I want to display the '<' character within some HTML text in a QMessageBox. It appears that Qt views the '<' character as the start of a tag and doesn't display it.

Displaying the following in a browser does work.

<html><</html>

Does anyone know if this is even possible in Qt?

The following code works for any characters I use except '<'.

QString str( QString( "Restricted characters found in device name: \"%1\"<br>" ).arg( sTypedName ) );
QString str2( QString( "Device name \"%1\" will become \"%2\".<br>" ).arg( sOldName ).arg( sNewName ) );
str.append( "<DIV align=\"center\"><TABLE>" );
str.append( "<TR><TD> Restricted:</TD><TD><b><</b></TD><TD><b>></b></TD></TR>" );
str.append( "<TR><TD>Replacement:</TD><TD><b>[</b></TD><TD><b>]</b></TD></TR>" );
str.append( "</TABLE></DIV><br>" );
str.append( str2 );
int rVal = QMessageBox::warning( this, tr("Restricted Characters Found"), str,
QMessageBox::Ok, QMessageBox::Cancel );
if ( rVal == QMessageBox::Cancel )
return false;

return true;

jacek
6th July 2007, 21:04
Displaying the following in a browser does work.

<html><</html>
But it shouldn't.

Use &lt; instead.

mclark
6th July 2007, 21:29
The "<html><<html>" was just an example. I saved it as a file and pointed IE to it. IE displayed "<". I was trying to show that IE can display an HTML '<'.

The problem I'm having is that I cannot display a '<' character in a QString with other HTML tags. If I replace the '<' with &lt in the original example, "&lt" is displayed, not '<'.

Or, when not using any HTML tags, Qt thinks a string containing '<' and '>' characters is HTML and may display incorrect characters. For example, if I have a string "Test<s>\nThis is a test.". The "<s>" will cause any following text ("This is a test.") to be struckout.

So, the question remains, can a '<' character be displayed via Qt as HTML?

A second question, can I display a QString with "<s>" or "<b>" or any other tag-like text in it and not have Qt treat it as HTML?

jacek
6th July 2007, 21:35
If I replace the '<' with &lt in the original example, "&lt" is displayed, not '<'.
Because it should be "&lt;", not "&lt".


A second question, can I display a QString with "<s>" or "<b>" or any other tag-like text in it and not have Qt treat it as HTML?
No, HTML standard says that everything between < and > is a tag. Use &lt; and &gt; instead.

mclark
6th July 2007, 21:43
Thanks jacek! I knew there had to be a way.

Who knew that semi-colons could be so important:o