Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 2

Qt-interest Archive, March 2008
html escaped string?


Message 1 in thread

Hi,

I need to html-escape a string in my Qt program. That is: I want to replace 
& by &amp;, < by &lt;, " by &quot; etc. I could of course code this 
manually, but I was wondering is this functionality isn't already available 
somewhere in Qt (4.4.0 beta)?
It strikes me as something that is probably available, but I can not find it 
so far...

Thanks,

André 

--
 [ signature omitted ] 

Message 2 in thread

On Wednesday 12 March 2008 15:48:07 André Somers wrote:
> Hi,
>
> I need to html-escape a string in my Qt program. That is: I want to replace
> & by &amp;, < by &lt;, " by &quot; etc. I could of course code this
> manually, but I was wondering is this functionality isn't already available
> somewhere in Qt (4.4.0 beta)?
> It strikes me as something that is probably available, but I can not find
> it so far...

QString escaped= 
QString(myhtml).replace("&","&amp;").replace(">","&gt;").replace("<","&lt;");

there is nothing more. everything else is valid html.
to avoid encoding issues with broken browsers you can use 
QUrl::toPercentEncoding  but i suggest just not using IE5.

> Thanks,
>
> André


-- 
 [ signature omitted ] 

Message 3 in thread

On Wednesday 12 March 2008 15:48:07 André Somers wrote:
> Hi,
>
> I need to html-escape a string in my Qt program. That is: I want to replace
> & by &amp;, < by &lt;, " by &quot; etc. I could of course code this
> manually, but I was wondering is this functionality isn't already available
> somewhere in Qt (4.4.0 beta)?
> It strikes me as something that is probably available, but I can not find
> it so far...

It's available, but slightly hidden.

It's Qt::escape, defined in <QTextDocument> (QtGui).

-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part.


Message 4 in thread

...and decoding?
&nbsp;
&aacute;
&aring;
etc...


On 3/12/08, Thiago Macieira <thiago.macieira@xxxxxxxxxxxxx> wrote:
> On Wednesday 12 March 2008 15:48:07 André Somers wrote:
>
> > Hi,
>  >
>  > I need to html-escape a string in my Qt program. That is: I want to replace
>  > & by &amp;, < by &lt;, " by &quot; etc. I could of course code this
>  > manually, but I was wondering is this functionality isn't already available
>  > somewhere in Qt (4.4.0 beta)?
>  > It strikes me as something that is probably available, but I can not find
>  > it so far...
>
>
> It's available, but slightly hidden.
>
>  It's Qt::escape, defined in <QTextDocument> (QtGui).
>
>
>  --
>  Thiago José Macieira - thiago.macieira AT trolltech.com
>  Trolltech ASA - Sandakerveien 116, NO-0402 Oslo, Norway
>
>

--
 [ signature omitted ] 

Message 5 in thread

Hi !

did you tried to use QTextDocument's toPlainText() or toHtml ?

Heiko

André Somers schrieb:
> Hi,
>
> I need to html-escape a string in my Qt program. That is: I want to 
> replace & by &amp;, < by &lt;, " by &quot; etc. I could of course code 
> this manually, but I was wondering is this functionality isn't already 
> available somewhere in Qt (4.4.0 beta)?
> It strikes me as something that is probably available, but I can not 
> find it so far...
>
> Thanks,
>
> André
> -- 
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with 
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>

-- 
 [ signature omitted ] 
begin:vcard
fn:Heiko Steindl
n:Steindl;Heiko
org:Computerhaus EDV-Handels Ges.m.b.H
adr;quoted-printable:;;Wienerstrasse 35a;Kapfenberg;Steiermark;8605;=C3=96sterreich
email;internet:heiko@xxxxxx
tel;work:+43 (0)3862 27777
tel;fax:+43 (0)3862 27777 77
tel;cell:+43 (0)676 3727777
x-mozilla-html:TRUE
url:http://www.kom.at
version:2.1
end:vcard


Message 6 in thread

Check this:
void QXmlStreamWriterPrivate::writeEscaped(const QString &s, bool 
escapeWhitespace)
{
    QString escaped;
    escaped.reserve(s.size());
    for ( int i = 0; i < s.size(); ++i ) {
        QChar c = s.at(i);
        if (c.unicode() == '<' )
            escaped.append(QLatin1String("&lt;"));
        else if (c.unicode() == '>' )
            escaped.append(QLatin1String("&gt;"));
        else if (c.unicode() == '&' )
            escaped.append(QLatin1String("&amp;"));
        else if (c.unicode() == '\"' )
            escaped.append(QLatin1String("&quot;"));
        else if (escapeWhitespace && c.isSpace()) {
            if (c.unicode() == '\n')
                escaped.append(QLatin1String("&#10;"));
            else if (c.unicode() == '\r')
                escaped.append(QLatin1String("&#13;"));
            else if (c.unicode() == '\t')
                escaped.append(QLatin1String("&#9;"));
            else
                escaped.inline_append(QLatin1Char(' '));
        } else {
            escaped.inline_append(c);
        }
    }
    if (device) {
#ifdef QT_NO_TEXTCODEC
        device->write(escaped.toLatin1(), escaped.size());
#else
        device->write(encoder->fromUnicode(escaped));
#endif
    }
    else if (stringDevice)
        stringDevice->append(escaped);
    else
        qWarning("QXmlStreamWriter: No device");
}

--
 [ signature omitted ] 

Message 7 in thread

To everybody who replied:

Thanks, these were very valuable suggestions. I have got it to work.

André


"André Somers" <andre@xxxxxxxxxxxxxxxx> wrote in message 
news:fr8qf8$ndm$1@xxxxxxxxxxxxxxxx
> Hi,
>
> I need to html-escape a string in my Qt program. That is: I want to 
> replace & by &amp;, < by &lt;, " by &quot; etc. I could of course code 
> this manually, but I was wondering is this functionality isn't already 
> available somewhere in Qt (4.4.0 beta)?
> It strikes me as something that is probably available, but I can not find 
> it so far...
>
> Thanks,
>
> André 

--
 [ signature omitted ]