Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Unexpected behavior in QTextEdit
Forum Updated to NodeBB v4.3 + New Features

Unexpected behavior in QTextEdit

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 528 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    drmhkelley
    wrote on last edited by
    #1

    I routinely use a QTextEdit-based logging widget for diagnostic or other output from any app I'm developing. Up until now, that had been used exclusively for plaintext data. However, I had recent occasion to dump some richtext formatted output to the QTextEditor, using insertHtml(). That worked mostly just fine. However, that flips the QTextEdit widget from "single-spaced" to "double-spaced" output. I haven't found anything that discusses control of anything like "line spacing" in a QTextEdit widget or its underlying QDocument object.

    Any suggestion why this happens and what I might do to reverse the behavior?

    Thanks

    M 1 Reply Last reply
    0
    • D drmhkelley

      I routinely use a QTextEdit-based logging widget for diagnostic or other output from any app I'm developing. Up until now, that had been used exclusively for plaintext data. However, I had recent occasion to dump some richtext formatted output to the QTextEditor, using insertHtml(). That worked mostly just fine. However, that flips the QTextEdit widget from "single-spaced" to "double-spaced" output. I haven't found anything that discusses control of anything like "line spacing" in a QTextEdit widget or its underlying QDocument object.

      Any suggestion why this happens and what I might do to reverse the behavior?

      Thanks

      M Offline
      M Offline
      mpergand
      wrote on last edited by
      #2

      @drmhkelley
      Try to insert with the cursor:

      QTextCursor cursor=textEdit->textCursor();
      cursor.movePosition(QTextCursor::End);
      cursor.insertHtml("string to append. ");
      

      not tested ...

      D 1 Reply Last reply
      0
      • M mpergand

        @drmhkelley
        Try to insert with the cursor:

        QTextCursor cursor=textEdit->textCursor();
        cursor.movePosition(QTextCursor::End);
        cursor.insertHtml("string to append. ");
        

        not tested ...

        D Offline
        D Offline
        drmhkelley
        wrote on last edited by
        #3

        @mpergand That is precisely how I do it.

        M 1 Reply Last reply
        0
        • D drmhkelley

          @mpergand That is precisely how I do it.

          M Offline
          M Offline
          mpergand
          wrote on last edited by
          #4

          @drmhkelley
          I can't reproduce what you described, Can you show us more code.

          D 1 Reply Last reply
          0
          • M mpergand

            @drmhkelley
            I can't reproduce what you described, Can you show us more code.

            D Offline
            D Offline
            drmhkelley
            wrote on last edited by
            #5

            @mpergand
            The code is very simple. I have logging widget, pMainOut, that just extends a QTextEdit widget a bit. In particular, it adds method log, which is essentially defined as

            void log(QString msg)
            {
            moveCursor(QTextCursor::End, QTextCursor::MoveAnchor);
            insertHtml(msg);SreenCapture.JPG
            return;
            }

            I get the described behavior with the following.

            log("line 1");
            log("line 2");
            log("line 3");

            • list item
              QString html = "<p>html line 1</p>"[link text](link url)
              "<p>html line 2</p>"
              "<p>html line 3</p>";

            setHtml(true);
            log(html);
            setHtml(false);

            log("");
            log("line 4");
            log("line 5");
            log("line 6");

            I have attempted to attach a screen shot showing that the first three lines are single-spaced, but the remaining ones double-spaces. Incidentally, when I cut/past the contents of the output window as plaintext, there no extre linefeeds. The double spacing is not from extraneous blank lines, but just single lines display as double-spaced.

            Curious.

            D M 2 Replies Last reply
            0
            • D drmhkelley

              @mpergand
              The code is very simple. I have logging widget, pMainOut, that just extends a QTextEdit widget a bit. In particular, it adds method log, which is essentially defined as

              void log(QString msg)
              {
              moveCursor(QTextCursor::End, QTextCursor::MoveAnchor);
              insertHtml(msg);SreenCapture.JPG
              return;
              }

              I get the described behavior with the following.

              log("line 1");
              log("line 2");
              log("line 3");

              • list item
                QString html = "<p>html line 1</p>"[link text](link url)
                "<p>html line 2</p>"
                "<p>html line 3</p>";

              setHtml(true);
              log(html);
              setHtml(false);

              log("");
              log("line 4");
              log("line 5");
              log("line 6");

              I have attempted to attach a screen shot showing that the first three lines are single-spaced, but the remaining ones double-spaces. Incidentally, when I cut/past the contents of the output window as plaintext, there no extre linefeeds. The double spacing is not from extraneous blank lines, but just single lines display as double-spaced.

              Curious.

              D Offline
              D Offline
              drmhkelley
              wrote on last edited by
              #6

              @drmhkelley
              Sorry - I was a bit sloppy with that. And I left out an important point for the "log" method. It has a switch that depends on whether the logged text is plaintext or html. If html, then it uses insertHtml() as shown. If plaintext, it uses insertPlaintext() instead.

              1 Reply Last reply
              0
              • D drmhkelley

                @mpergand
                The code is very simple. I have logging widget, pMainOut, that just extends a QTextEdit widget a bit. In particular, it adds method log, which is essentially defined as

                void log(QString msg)
                {
                moveCursor(QTextCursor::End, QTextCursor::MoveAnchor);
                insertHtml(msg);SreenCapture.JPG
                return;
                }

                I get the described behavior with the following.

                log("line 1");
                log("line 2");
                log("line 3");

                • list item
                  QString html = "<p>html line 1</p>"[link text](link url)
                  "<p>html line 2</p>"
                  "<p>html line 3</p>";

                setHtml(true);
                log(html);
                setHtml(false);

                log("");
                log("line 4");
                log("line 5");
                log("line 6");

                I have attempted to attach a screen shot showing that the first three lines are single-spaced, but the remaining ones double-spaces. Incidentally, when I cut/past the contents of the output window as plaintext, there no extre linefeeds. The double spacing is not from extraneous blank lines, but just single lines display as double-spaced.

                Curious.

                M Offline
                M Offline
                mpergand
                wrote on last edited by mpergand
                #7

                @drmhkelley said in Unexpected behavior in QTextEdit:

                "<p>html line 2</p>"
                "<p>html line 3</p>";

                try with <br> instead:
                "html line 1<br>"

                D 1 Reply Last reply
                0
                • M mpergand

                  @drmhkelley said in Unexpected behavior in QTextEdit:

                  "<p>html line 2</p>"
                  "<p>html line 3</p>";

                  try with <br> instead:
                  "html line 1<br>"

                  D Offline
                  D Offline
                  drmhkelley
                  wrote on last edited by
                  #8

                  @mpergand Thanks for that!! I can't begin to understand how or why that works, but it removed the symptom.

                  However, my main take-away is that at least one (and likely more) html tag has unexpected effects on the QTextEdit widget. I'll wait to see how it behaves with a variety of more complicated html outputs to declare this a solved issue.

                  JonBJ 1 Reply Last reply
                  0
                  • jeremy_kJ Offline
                    jeremy_kJ Offline
                    jeremy_k
                    wrote on last edited by
                    #9

                    QTextEdit converts the inserted html and plain text. The spacing is due to the style that is automatically applied unless the inserted html already specifies styling. Have a look at the output of QTextEdit::toHtml().

                    Asking a question about code? http://eel.is/iso-c++/testcase/

                    1 Reply Last reply
                    0
                    • D drmhkelley

                      @mpergand Thanks for that!! I can't begin to understand how or why that works, but it removed the symptom.

                      However, my main take-away is that at least one (and likely more) html tag has unexpected effects on the QTextEdit widget. I'll wait to see how it behaves with a variety of more complicated html outputs to declare this a solved issue.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #10

                      @drmhkelley
                      I don't understand. Naturally where you used <p>...</p> you got "extra" spacing as these are HTML paragraphs, so you get a blank line afterwards. (You could verify it is not line spacing by making your text in each case much longer so it spills across multiple lines.)

                      That was never the problem. The issue was that your output shows that after the last <p>html line 3</p> your future line 4 etc. still had "paragraph gaps" where the earlier line 3 etc. did not.

                      There is some indication from your output & code that once you have put in a <p>...</p> the text edit gets mixed up from then onwards.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved