Issues with blank lines and line breaks in QTextEdit
-
My TextEdit content:
wwwwwwwwwwww
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::cursorPositionChangedevent 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> -
Hi,
Which version of Qt are you using ?
On which platform ?
Please provide minimal compilable example that reproduces this behaviour. -
Hi,
Which version of Qt are you using ?
On which platform ?
Please provide minimal compilable example that reproduces this behaviour.

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.
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() { } };
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(); } -


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.
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() { } };
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(); } -
@cuijg
Although you have shown what version of Qt Creator you are using, what version of Qt are you compiling/linking with? -
@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

