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. Issues with blank lines and line breaks in QTextEdit

Issues with blank lines and line breaks in QTextEdit

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 150 Views 1 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.
  • C Offline
    C Offline
    cuijg
    wrote last edited by
    #1

    My TextEdit content:
    wwwwww

    wwwwww

    Problem: Pressing Enter once on a line with content creates a new line, but pressing Enter twice on a new blank line creates a new line. Furthermore, only the last blank line has a 108% line spacing set; the other blank lines do not.

    I listened to the QTextEdit::cursorPositionChanged event and set the line spacing in the event handler:

    void onSigCursorPositionChanged() {
    
    QTextCursor cursor = this->textCursor();
    
    QTextBlockFormat format = cursor.blockFormat();
    
    if (format.lineHeight() != 108) {
    
    format.setLineHeight(108, QTextBlockFormat::ProportionalHeight);
    
    format.setTopMargin(0);
    
    format.setBottomMargin(0);
    
    cursor.setBlockFormat(format);
    
    }
    }
    

    This is the content saved to the file via ui->textedit->toHtml():

    <!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; }
    li.unchecked::marker { content: "\2610"; }
    li.checked::marker { content: "\2612"; }
    </style></head><body style=" font-family:'.AppleSystemUIFont'; font-size:16pt; 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; line-height:108%;">wwww</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;"><br /></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;"><br /></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;"><br /></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;"><br /></p>
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:108%;">wwww</p></body></html>
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote last edited by
      #2

      Hi,

      Which version of Qt are you using ?
      On which platform ?
      Please provide minimal compilable example that reproduces this behaviour.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      C 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Which version of Qt are you using ?
        On which platform ?
        Please provide minimal compilable example that reproduces this behaviour.

        C Offline
        C Offline
        cuijg
        wrote last edited by cuijg
        #3

        @SGaist
        e9d3a4ac-cb15-4db2-89a0-1688821c3690-image.png

        563115cc-780f-4edb-a1e3-38c3ee45ad67-image.png
        1c2f2c68-fbe1-4996-b37b-e6e2af2c5f50-image.png

        I found that as long as I use the default line spacing without modifying it myself, there won't be any issues with line breaks.

        @SGaist

        class MyTextEdit : public QTextEdit {
            Q_OBJECT
        public:
            MyTextEdit(QWidget *parent = nullptr)
                : QTextEdit(parent)
            {
                this->setMouseTracking(true);
        
                int font_id = QFontDatabase::addApplicationFont(":/font/DIN-Medium.ttf");
                QStringList fontFamilies = QFontDatabase::applicationFontFamilies(font_id);
                QString font_family_name = fontFamilies.at(0);
                QFont customFont(font_family_name, 16);
                this->setFont(customFont);
        
                this->setStyleSheet("color: #34304b;");
                this->document()->setDefaultStyleSheet("p { line-height:108%; }");
        
                connect(this, &QTextEdit::cursorPositionChanged, this, &MyTextEdit::onSigCursorPositionChanged);
                connect(this, &QTextEdit::textChanged, this, &MyTextEdit::onSigTextChanged);
            }
        
        protected:
            void insertFromMimeData(const QMimeData *source) override {
        
                QTextEdit::insertFromMimeData(source);
            }
        
            void keyPressEvent(QKeyEvent *e) override {
                QTextEdit::keyPressEvent(e);
        
                if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
        
                }
            }
        
        protected slots:
            void onSigCursorPositionChanged() {
                QTextCursor cursor = this->textCursor();
                QTextBlockFormat format = cursor.blockFormat();
                if (format.lineHeight() != 108) {
                    format.setLineHeight(108, QTextBlockFormat::ProportionalHeight);
                    cursor.setBlockFormat(format);
                }
            }
        
            void onSigTextChanged() {
        
            }
        };
        

        f0b7cb37-6703-4f66-85e5-fdb44dfce204-image.png

        save and load

        void TestMainWindow::onsig_btn0_clicked()
        {
            QFile file(DATA_STORE_DIR() + "test.data");
            if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
                qWarning() << "无法打开文件";
                return;
            }
        
            QString htmlContent = ui->textEdit_3->toHtml();
        
            QTextStream out(&file);
            out.setEncoding(QStringConverter::Encoding::Utf8); // 确保使用 UTF-8 编码以支持中文
            out << htmlContent;
        
            file.close();
        }
        
        void TestMainWindow::onsig_btn1_clicked()
        {
            QFile file(DATA_STORE_DIR() + "test.data");
            if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
                qWarning() << "无法打开文件";
                return;
            }
        
            QTextStream in(&file);
            in.setEncoding(QStringConverter::Encoding::Utf8); // 确保使用 UTF-8 编码
            QString htmlContent = in.readAll();
            ui->textEdit_3->setHtml(htmlContent);
            file.close();
        
            //ui->textEdit->fix_image_alignment();
        }
        
        JonBJ 1 Reply Last reply
        0
        • C cuijg

          @SGaist
          e9d3a4ac-cb15-4db2-89a0-1688821c3690-image.png

          563115cc-780f-4edb-a1e3-38c3ee45ad67-image.png
          1c2f2c68-fbe1-4996-b37b-e6e2af2c5f50-image.png

          I found that as long as I use the default line spacing without modifying it myself, there won't be any issues with line breaks.

          @SGaist

          class MyTextEdit : public QTextEdit {
              Q_OBJECT
          public:
              MyTextEdit(QWidget *parent = nullptr)
                  : QTextEdit(parent)
              {
                  this->setMouseTracking(true);
          
                  int font_id = QFontDatabase::addApplicationFont(":/font/DIN-Medium.ttf");
                  QStringList fontFamilies = QFontDatabase::applicationFontFamilies(font_id);
                  QString font_family_name = fontFamilies.at(0);
                  QFont customFont(font_family_name, 16);
                  this->setFont(customFont);
          
                  this->setStyleSheet("color: #34304b;");
                  this->document()->setDefaultStyleSheet("p { line-height:108%; }");
          
                  connect(this, &QTextEdit::cursorPositionChanged, this, &MyTextEdit::onSigCursorPositionChanged);
                  connect(this, &QTextEdit::textChanged, this, &MyTextEdit::onSigTextChanged);
              }
          
          protected:
              void insertFromMimeData(const QMimeData *source) override {
          
                  QTextEdit::insertFromMimeData(source);
              }
          
              void keyPressEvent(QKeyEvent *e) override {
                  QTextEdit::keyPressEvent(e);
          
                  if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
          
                  }
              }
          
          protected slots:
              void onSigCursorPositionChanged() {
                  QTextCursor cursor = this->textCursor();
                  QTextBlockFormat format = cursor.blockFormat();
                  if (format.lineHeight() != 108) {
                      format.setLineHeight(108, QTextBlockFormat::ProportionalHeight);
                      cursor.setBlockFormat(format);
                  }
              }
          
              void onSigTextChanged() {
          
              }
          };
          

          f0b7cb37-6703-4f66-85e5-fdb44dfce204-image.png

          save and load

          void TestMainWindow::onsig_btn0_clicked()
          {
              QFile file(DATA_STORE_DIR() + "test.data");
              if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
                  qWarning() << "无法打开文件";
                  return;
              }
          
              QString htmlContent = ui->textEdit_3->toHtml();
          
              QTextStream out(&file);
              out.setEncoding(QStringConverter::Encoding::Utf8); // 确保使用 UTF-8 编码以支持中文
              out << htmlContent;
          
              file.close();
          }
          
          void TestMainWindow::onsig_btn1_clicked()
          {
              QFile file(DATA_STORE_DIR() + "test.data");
              if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
                  qWarning() << "无法打开文件";
                  return;
              }
          
              QTextStream in(&file);
              in.setEncoding(QStringConverter::Encoding::Utf8); // 确保使用 UTF-8 编码
              QString htmlContent = in.readAll();
              ui->textEdit_3->setHtml(htmlContent);
              file.close();
          
              //ui->textEdit->fix_image_alignment();
          }
          
          JonBJ Online
          JonBJ Online
          JonB
          wrote last edited by
          #4

          @cuijg
          Although you have shown what version of Qt Creator you are using, what version of Qt are you compiling/linking with?

          C 1 Reply Last reply
          0
          • C Offline
            C Offline
            cuijg
            wrote last edited by
            #5

            To avoid affecting the user experience, I removed the code for custom line spacing and replaced it with a font with larger character spacing.

            1 Reply Last reply
            0
            • JonBJ JonB

              @cuijg
              Although you have shown what version of Qt Creator you are using, what version of Qt are you compiling/linking with?

              C Offline
              C Offline
              cuijg
              wrote last edited by
              #6

              @JonB
              These two don't need to be consistent, right?

              50ea4a88-b45a-4f9c-8a8e-c2050b57a084-image.png

              jsulmJ 1 Reply Last reply
              0
              • C cuijg

                @JonB
                These two don't need to be consistent, right?

                50ea4a88-b45a-4f9c-8a8e-c2050b57a084-image.png

                jsulmJ Online
                jsulmJ Online
                jsulm
                Lifetime Qt Champion
                wrote last edited by
                #7

                @cuijg said in Issues with blank lines and line breaks in QTextEdit:

                These two don't need to be consistent

                No, they don't have to

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                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