2

Qt / C++コードがあります:

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

以下を出力します。

 === 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 SPARTA</p></td></tr></table></body></html>" 

ご覧のとおり、QTextDocumentにはデフォルトのCSSのようなものがあります。

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

しかし、pタグと複数の空白を使用してHTMLを設定すると、空白が削除されます。問題は-なぜですか?もう1つの質問は、なぜpタグにマージンを追加するのかということです。

PS行を追加すれば問題なく動作します

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

setHtmlを実行する前に-複数の空白は削除されません。しかし、このスタイルタグは何ですか?デフォルトのスタイルシートではありませんか?なぜQtはそれを無視するのですか?

答えてくれてありがとう。

4

2 に答える 2

4

この動作はhtmlからのもので、htmlはタグ内の複数のスペースを無視するだけです..

スペースに特別なコードを使用してみてください:&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();
}
于 2010-06-10T18:13:50.997 に答える
0

ドキュメントには、リッチ テキスト オブジェクトでサポートされている CSS タグが記載されています。おそらく、バグトラッカーでバグを報告する必要がありますか?

于 2010-09-16T21:53:05.277 に答える