views:

139

answers:

2

There is a 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      SPARTA</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"&gt;
<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"&gt;
<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 SPARTA</p></td></tr></table></body></html>" 

As I can see, there is something like default CSS in QTextDocument:

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

But, when I'm setting HTML with p tag and multiple whitespaces, it removes whitespaces. The question is - why? Another question is - why is it adding margins to p tag?

PS It works fine if I add a line

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

before doing setHtml - it doesn't remove multiple whitespaces. But what is this style-tag then? Isn't it a default style sheet? Why Qt ignores it?

Thanks for the answer.

+1  A: 

this behavior is from html, html just ignore more than one spaces inside tags..

try to use the special code for space: &nbsp;

#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());

    QByteArray myhtml ="<p>THIS       IS      SPARTA</p>";

    doc->setHtml(myhtml.replace(" ","&nbsp;"));

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

    return a.exec();
}
olarva
I know it, but there is a CSS with 'white-space: pre-wrap' for <p> and <li> tags, so white-spaces mustn't be removed. That's the problem! :(
Eol
A: 

The documentation tells you which CSS tags are supported in a Rich Text object - white-space: pre-wrap is supposed to be supported, at least in the Qt 4.7 docs I have. Perhaps you should raise a bug in the bug tracker?

anon qt newbie
Yep, it is supported. And when I set it as default style sheet, it works with it fine. The problem is it ignores it's own default CSS. Maybe someone should fill a bugreport then.. I just don't know, should Qt work with it or not (I mean, is it bug or feature ;) )
Eol
Personally I'd report it and see what the proper behaviour is supposed to be. Perhaps it's as simple as a doc bug, or maybe it's a real bug. You could try digging into the source and find out :-)
anon qt newbie