Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-80473

Inserting an <HR> tag into QTextDocument does Bad Things

    XMLWordPrintable

Details

    • Bug
    • Resolution: Invalid
    • P2: Important
    • None
    • 5.15.6, 6.2.0
    • GUI: Text handling
    • None
    • dbb9579566f3accd8aa5fe61db9692991117afd3

    Description

      I want to put together a QTextDocument from multiple QTextDocumentFragments, separated by horizontal rules represented by <HR>. Something like this (as an ASCII mock-up):

      foo
      -------
      bar
      -------
      baz

      My attempt to do this (using insertText() instead of insertFragment() for now, to try to get the basic scheme working, but the same thing happens with insertFragment()) looks like this:

      QTextEdit *textedit = ui->descriptionTextEdit;
      QTextDocument *textdoc = textedit->document();

      textedit->clear();

      QTextCursor insertion(textdoc);
      insertion.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor);

      insertion.insertText("foo");
      insertion.insertBlock();
      insertion.insertHtml("<HR>");
      insertion.insertBlock();
      insertion.insertText("bar");
      insertion.insertBlock();
      insertion.insertHtml("<HR>");
      insertion.insertBlock();
      insertion.insertText("baz");

      qDebug() << textdoc->toHtml();

      The results from this are truly bizarre. You can see a screenshot at stackoverflow at https://stackoverflow.com/questions/59009040/assembling-a-qtextdocument-from-fragments. As you can see, I requested two <HR> tags but I got five horizontal rules! The HTML for the QTextDocument is bizarre too (I've added a couple of newlines to make it more readable):

      <!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:'.SF NS Text'; font-size:13pt; font-weight:400; font-style:normal;\">
      <p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">foo</p>
      <p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">bar</p>
      <p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">baz</p>
      </body></html>

      There is no sign of my <HR> tags in the HTML at all. With slightly different strategies – called insertBlock() only before the <HR> insertions, or only after them, or not at all – I get variations on this theme; sometimes bar and baz are not visible in the HTML output, but the abundance of <HR> tags are, instead. For example, if I change the insertion code to this:

      insertion.insertText("foo");
      //insertion.insertBlock();
      insertion.insertHtml("<HR>");
      insertion.insertBlock();
      insertion.insertText("bar");
      //insertion.insertBlock();
      insertion.insertHtml("<HR>");
      insertion.insertBlock();
      insertion.insertText("baz");
      then I get a result that has four horizontal rules in the visible rendering in my app, and has this HTML output:

      <!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:'.SF NS Text'; font-size:13pt; font-weight:400; font-style:normal;\">
      <p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">foo</p>
      <hr />
      <hr />
      <hr />
      <hr />
      </body></html>

      Note that bar and baz are not visible in the HTML output, although they do render visibly in the app.

      But in no permutation of the code that I have found do I get what I want. It looks like I am putting QTextDocument into some kind of internally inconsistent or confused state. I don't know whether there is a "right" way to do this – my stackoverflow question has gotten no good answer yet – but it seems like my attempts above clearly indicate a bug in QTextDocument, in any case.

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              srutledg Shawn Rutledge
              bhaller Ben Haller
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes