Results 1 to 6 of 6

Thread: QTextDocument eats whitespacess

  1. #1
    Join Date
    Jun 2010
    Posts
    3
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Question QTextDocument eats whitespacess

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QTextDocument eats whitespacess

    About the white spaces, that's normal because that's how html works.
    See non breaking spaces to add more spaces between words (&nbsp;)
    Last edited by tbscope; 8th June 2010 at 10:49. Reason: Disable smilies

  3. #3
    Join Date
    Jun 2010
    Posts
    3
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextDocument eats whitespacess

    Quote Originally Posted by tbscope View Post
    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QTextDocument eats whitespacess

    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
    Last edited by tbscope; 8th June 2010 at 10:47.

  5. #5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QTextDocument eats whitespacess

    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.

  6. #6
    Join Date
    Jun 2010
    Posts
    3
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextDocument eats whitespacess

    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?
    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
    Last edited by Eol; 8th June 2010 at 12:48.

Similar Threads

  1. Print of QTextDocument
    By tsschulz in forum Qt Programming
    Replies: 1
    Last Post: 2nd November 2008, 11:18
  2. QTextDocument color
    By bunjee in forum Qt Programming
    Replies: 9
    Last Post: 25th April 2008, 19:54
  3. QTextDocument Html
    By bunjee in forum Qt Programming
    Replies: 1
    Last Post: 17th November 2007, 13:50
  4. QTextDocument?
    By thomasjoy in forum Qt Programming
    Replies: 1
    Last Post: 15th August 2007, 23:42
  5. QTextDocument stinks
    By elcuco in forum Qt Programming
    Replies: 4
    Last Post: 24th July 2007, 13:30

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.