[Qt-interest] [Qt-Interest] QGraphicsTextItem.setHtml() does not work as expected

Long Cheng kevinclcn at gmail.com
Wed Jun 2 10:26:14 CEST 2010


I set html as below, but text->toHtml returns a different string.

QGraphicsTextItem text = new QGraphicsTextItem();
text->setHtml("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN"
"http://www.w3.org/TR/REC-html40/strict.dtd\">"
      "<html><head><meta name=\"qrichtext\" content=\"1\" /><style
type=\"text/css\">"
      "p, li { white-space: pre-wrap; }"
      "</style></head><body style=\" font-family:'Times';
font-size:10pt; font-weight:600; font-style:normal;\">"
      "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px;
margin-right:0px; -qt-block-indent:0; text-indent:0px;\">This is a
QGraphicsTextItem sample</p></body></html>\"");

String returned by text->toHtml():

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
"http://www.w3.org/TR/REC-html40/strict.dtd">
<html><head><meta name="qrichtext" content="1" /><style type="text/css">
p, li { white-space: pre-wrap; }
</style></head><body style=" font-family:'MS Shell Dlg 2';
font-size:8.25pt; font-weight:400; font-style:normal;">
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px;
margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style="
font-family:'Times'; font-size:10pt; font-weight:600;">This is a
QGraphicsTextItem sample</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px;
margin-right:0px; -qt-block-indent:0;
text-indent:0px;">"</p></body></html>

As you can see, the "font-family:"Times"" is applied to a span rather
than the body. So when I set font with text->setFont(), it won't
affect the string.

Do you know how to set the QGraphicsTextItem with the exact html I specified.

Thank you!
Long



More information about the Qt-interest-old mailing list