Hello!
I have a simple Qt/C++ code:
Qt Code:
  1. #include <QtCore/QCoreApplication>
  2. #include <QtGui/QTextDocument>
  3. #include <QByteArray>
  4. #include <QDebug>
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8. QCoreApplication a(argc, argv);
  9.  
  10. qDebug() << " === Document was: === ";
  11. qDebug() << doc->toHtml(QByteArray());
  12.  
  13. doc->setHtml("<p>THIS IS TEST</p>");
  14.  
  15. qDebug() << " === Document now: === ";
  16. qDebug() << doc->toHtml(QByteArray());
  17.  
  18. return a.exec();
  19. }
To copy to clipboard, switch view to plain text mode 

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
Qt Code:
  1. doc->setDefaultStyleSheet("p, li { white-space: pre-wrap; }");
To copy to clipboard, switch view to plain text mode 
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?
And another question is why did appear this margins in p tag (top and bottom, 12px)?
Thanks in advance for your response.