PDA

View Full Version : QTextDocument eats whitespacess



Eol
8th June 2010, 09:07
Hello!
I have a simple Qt/C++ code:


#include <QtCore/QCoreApplication>
#include <QtGui/QTextDocument>
#include <QByteArray>
#include <QDebug>

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTextDocument *doc = new QTextDocument();

qDebug() << " === Document was: === ";
qDebug() << doc->toHtml(QByteArray());

doc->setHtml("<p>THIS IS TEST</p>");

qDebug() << " === Document now: === ";
qDebug() << doc->toHtml(QByteArray());

return a.exec();
}


It outputs:

=== Document was: ===
"<!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:'Helvetica'; font-size:12pt; font-weight:400; font-style:normal;">
<table style="-qt-table-type: root; margin-top:4px; margin-bottom:4px; margin-left:4px; margin-right:4px;">
<tr>
<td style="border: none;">
<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"></p></td></tr></table></body></html>"
=== Document now: ===
"<!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:'Helvetica'; font-size:12pt; font-weight:400; font-style:normal;">
<table style="-qt-table-type: root; margin-top:4px; margin-bottom:4px; margin-left:4px; margin-right:4px;">
<tr>
<td style="border: none;">
<p style=" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">THIS IS TEST</p></td></tr></table></body></html>"
There is something like default CSS in QTextDocument:


<style type="text/css">
p, li { white-space: pre-wrap; }
</style>

The problem is that it eats multiple whitespaces when I'm adding string with <p> tag and multiple whitespaces. Why?
Possible way to solve this problem is to call


doc->setDefaultStyleSheet("p, li { white-space: pre-wrap; }");

before calling setHtml - in this case all works fine. Is it a correct solution or just hack? :)
And I wonder why it ignores its standart CSS? :confused:
And another question is why did appear this margins in p tag (top and bottom, 12px)?
Thanks in advance for your response.

tbscope
8th June 2010, 09:18
About the white spaces, that's normal because that's how html works.
See non breaking spaces to add more spaces between words (&nbsp;)

Eol
8th June 2010, 09:50
About the white spaces, that's normal because that's how html works.
See non breaking spaces to add more spaces between words (&nbsp;)

Thanks for your reply :)
I know about whitespaces, but there is CSS which sets 'white-space: pre-wrap' option to <p> and <li> tags. That means that text in this tags is preformatted and Qt mustn't ignore multiple whitespaces in it.

tbscope
8th June 2010, 10:36
white-space is not supported:
http://doc.qt.nokia.com/4.6/richtext-html-subset.html

Edit: Ignore this post, I should read better, it actually is supported

tbscope
8th June 2010, 10:51
Put the default style sheet before inserting new html text.

Quote from the documentation:

Changing the default style sheet does not have any effect to the existing content of the document.

Eol
8th June 2010, 12:15
Thanks, this helps. But what is this style tag? Isn't it default style-sheet? Why Qt ignores it when adding HTML? Is it bug or feature? :p
And what about margins? Why did they appear there?

Edit:
But if I'm adding text without <p> tags (QTextDocument adds it itself!), setDefaultStyleSheet doesn't help - it removes multiple whitespaces anyway :(