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. QTextEdit loses rich text formatting after pressing enter
QtWS25 Last Chance

QTextEdit loses rich text formatting after pressing enter

Scheduled Pinned Locked Moved Solved General and Desktop
qtexteditrich text
8 Posts 2 Posters 1.1k Views
  • 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.
  • T Offline
    T Offline
    tapsbrighton
    wrote on last edited by
    #1

    Can someone please help me. I have setup a simple QTextEdit and it accepts rich text. And whenever I press enter after the text that has a certain formatting, the cursor goes to the next line as expected. But when I press enter again, the cursor doesn't move. And when I press enter again, the cursor moves down but when it does so, it loses the text formatting that was applied. You can see it decrease in size and when I type new text it doesn't have any formatting.
    How can I stop this behaviour? I was it to retain the previous formatting no matter how many empty lines I put by pressing enter.

    The code for the setup of the QTextEdit is like this:

    #include <QTextEdit>
    
    Editor2::Editor2(QWidget *parent) : QTextEdit(parent) {
    
        setAcceptRichText(true);
        showMaximized();
    
        setHtml("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
                "<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
                "p, li { white-space: pre-wrap; }\n"
                "</style></head><body style=\" font-family:'Comic Relief'; font-size:15px; font-weight:600; font-style:normal;\">\n"
                "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;\"><span style=\" font-family:'Sanlulus'; font-size:22pt; font-weight:400; color:#00ffff; background-color:#aa0000;\">Will you shine and light the brightest</span></p>\n"
                "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;\"><span style=\" font-family:'Sanlulus'; font-size:22pt; font-weight:400; color:#00ffff; background-color:#aa0000;\">Will you shine and light the brightest</span></p>\n"
                "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;\"><span style=\" font-family:'Sanlulus'; font-size:22pt; font-weight:400; color:#00ffff; background-color:#aa0000;\">Will you shine and light the brightest</span></p>\n"
                "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;\"><span style=\" font-family:'Sanlulus'; font-size:22pt; font-weight:400; color:#00ffff; background-color:#aa0000;\">Will you shine and light the brightest</span></p>\n"
                "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;\"><span style=\" font-family:'Sanlulus'; font-size:22pt; font-weight:400; color:#00ffff; background-color:#aa0000;\">Will you shine and light the brightest</span></p></body></html>");
    }
    
    JonBJ 1 Reply Last reply
    0
    • JonBJ JonB

      @tapsbrighton
      Well, at least that explains why it shows as it does, right? But not why that happened, or whether you can do anything about it.

      T Offline
      T Offline
      tapsbrighton
      wrote on last edited by
      #5

      @JonB I reimplemented the keyPressEvent and it seems to be working.

      void Editor2::keyPressEvent(QKeyEvent *e)
      {
          if(e->key()==Qt::Key_Return){
              qInfo()<<"Enter key pressed";
      
              append("");
      
          }else{
              QTextEdit::keyPressEvent(e);
          }
      }
      
      JonBJ 1 Reply Last reply
      0
      • T tapsbrighton

        Can someone please help me. I have setup a simple QTextEdit and it accepts rich text. And whenever I press enter after the text that has a certain formatting, the cursor goes to the next line as expected. But when I press enter again, the cursor doesn't move. And when I press enter again, the cursor moves down but when it does so, it loses the text formatting that was applied. You can see it decrease in size and when I type new text it doesn't have any formatting.
        How can I stop this behaviour? I was it to retain the previous formatting no matter how many empty lines I put by pressing enter.

        The code for the setup of the QTextEdit is like this:

        #include <QTextEdit>
        
        Editor2::Editor2(QWidget *parent) : QTextEdit(parent) {
        
            setAcceptRichText(true);
            showMaximized();
        
            setHtml("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
                    "<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
                    "p, li { white-space: pre-wrap; }\n"
                    "</style></head><body style=\" font-family:'Comic Relief'; font-size:15px; font-weight:600; font-style:normal;\">\n"
                    "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;\"><span style=\" font-family:'Sanlulus'; font-size:22pt; font-weight:400; color:#00ffff; background-color:#aa0000;\">Will you shine and light the brightest</span></p>\n"
                    "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;\"><span style=\" font-family:'Sanlulus'; font-size:22pt; font-weight:400; color:#00ffff; background-color:#aa0000;\">Will you shine and light the brightest</span></p>\n"
                    "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;\"><span style=\" font-family:'Sanlulus'; font-size:22pt; font-weight:400; color:#00ffff; background-color:#aa0000;\">Will you shine and light the brightest</span></p>\n"
                    "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;\"><span style=\" font-family:'Sanlulus'; font-size:22pt; font-weight:400; color:#00ffff; background-color:#aa0000;\">Will you shine and light the brightest</span></p>\n"
                    "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;\"><span style=\" font-family:'Sanlulus'; font-size:22pt; font-weight:400; color:#00ffff; background-color:#aa0000;\">Will you shine and light the brightest</span></p></body></html>");
        }
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #2

        @tapsbrighton
        Not sure what your <Enters>/newlines are doing. You show the HTML you initially put into the QTextEdit. It would be interesting to see what that HTML ends up as when you do as you say and it goes wrong.

        T 1 Reply Last reply
        0
        • JonBJ JonB

          @tapsbrighton
          Not sure what your <Enters>/newlines are doing. You show the HTML you initially put into the QTextEdit. It would be interesting to see what that HTML ends up as when you do as you say and it goes wrong.

          T Offline
          T Offline
          tapsbrighton
          wrote on last edited by
          #3

          @JonB The first line 'After pressing enter once' has the formatting as the text above it. However the last one, 'After pressing enter twice' has lost all formatting

          <!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" /><meta charset="utf-8" /><style type="text/css">
          p, li { white-space: pre-wrap; }
          hr { height: 1px; border-width: 0; }
          </style></head><body style=" font-family:'Segoe UI'; font-size:9pt; 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; -qt-user-state:0;"><span style=" font-family:'Sanlulus'; font-size:22pt; color:#00ffff; background-color:#aa0000;">Will you shine and light the brightest</span></p>
          <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Sanlulus'; font-size:22pt; color:#00ffff; background-color:#aa0000;">Will you shine and light the brightest</span></p>
          <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Sanlulus'; font-size:22pt; color:#00ffff; background-color:#aa0000;">Will you shine and light the brightest</span></p>
          <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Sanlulus'; font-size:22pt; color:#00ffff; background-color:#aa0000;">Will you shine and light the brightest</span></p>
          <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Sanlulus'; font-size:22pt; color:#00ffff; background-color:#aa0000;">Will you shine and light the brightest</span></p>
          <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'Sanlulus'; font-size:22pt; color:#00ffff; background-color:#aa0000;">After pressing enter once</span></p>
          <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; font-family:'Sanlulus'; font-size:22pt; color:#00ffff;"><br /></p>
          <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">After pressing enter twice</p></body></html>
          
          JonBJ 1 Reply Last reply
          0
          • T tapsbrighton

            @JonB The first line 'After pressing enter once' has the formatting as the text above it. However the last one, 'After pressing enter twice' has lost all formatting

            <!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" /><meta charset="utf-8" /><style type="text/css">
            p, li { white-space: pre-wrap; }
            hr { height: 1px; border-width: 0; }
            </style></head><body style=" font-family:'Segoe UI'; font-size:9pt; 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; -qt-user-state:0;"><span style=" font-family:'Sanlulus'; font-size:22pt; color:#00ffff; background-color:#aa0000;">Will you shine and light the brightest</span></p>
            <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Sanlulus'; font-size:22pt; color:#00ffff; background-color:#aa0000;">Will you shine and light the brightest</span></p>
            <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Sanlulus'; font-size:22pt; color:#00ffff; background-color:#aa0000;">Will you shine and light the brightest</span></p>
            <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Sanlulus'; font-size:22pt; color:#00ffff; background-color:#aa0000;">Will you shine and light the brightest</span></p>
            <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Sanlulus'; font-size:22pt; color:#00ffff; background-color:#aa0000;">Will you shine and light the brightest</span></p>
            <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'Sanlulus'; font-size:22pt; color:#00ffff; background-color:#aa0000;">After pressing enter once</span></p>
            <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; font-family:'Sanlulus'; font-size:22pt; color:#00ffff;"><br /></p>
            <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">After pressing enter twice</p></body></html>
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #4

            @tapsbrighton
            Well, at least that explains why it shows as it does, right? But not why that happened, or whether you can do anything about it.

            T 1 Reply Last reply
            0
            • JonBJ JonB

              @tapsbrighton
              Well, at least that explains why it shows as it does, right? But not why that happened, or whether you can do anything about it.

              T Offline
              T Offline
              tapsbrighton
              wrote on last edited by
              #5

              @JonB I reimplemented the keyPressEvent and it seems to be working.

              void Editor2::keyPressEvent(QKeyEvent *e)
              {
                  if(e->key()==Qt::Key_Return){
                      qInfo()<<"Enter key pressed";
              
                      append("");
              
                  }else{
                      QTextEdit::keyPressEvent(e);
                  }
              }
              
              JonBJ 1 Reply Last reply
              0
              • T tapsbrighton

                @JonB I reimplemented the keyPressEvent and it seems to be working.

                void Editor2::keyPressEvent(QKeyEvent *e)
                {
                    if(e->key()==Qt::Key_Return){
                        qInfo()<<"Enter key pressed";
                
                        append("");
                
                    }else{
                        QTextEdit::keyPressEvent(e);
                    }
                }
                
                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #6

                @tapsbrighton
                Well that simply discards the Return (why bother with append("")?). Which is OK if you are OK with that!

                T 1 Reply Last reply
                0
                • JonBJ JonB

                  @tapsbrighton
                  Well that simply discards the Return (why bother with append("")?). Which is OK if you are OK with that!

                  T Offline
                  T Offline
                  tapsbrighton
                  wrote on last edited by
                  #7

                  @JonB It adds the new line that was supposed to be added and keeps the current text formatting

                  T 1 Reply Last reply
                  0
                  • T tapsbrighton

                    @JonB It adds the new line that was supposed to be added and keeps the current text formatting

                    T Offline
                    T Offline
                    tapsbrighton
                    wrote on last edited by
                    #8

                    @tapsbrighton This is a better solution rather than append:

                    insertPlainText("\n");
                    
                    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