QTextDocument will enter a new paragraph only after "Enter" is pressed 3 times

88 Views Asked by At

After filling QTextDocument with two paragraphs separated with empty line, to insert a new paragraph with empty line between them I need to press Enter button 3 times, instead of two expected. This happens with Qt 6.3.1. Do I do something wrong, or this is an issue of Qt?

#include <QTextEdit>
#include <QApplication>
#include <QTextDocument>

void fillTextDocument(QTextDocument *doc, const QString &text)
{
    QTextCursor c(doc);

    c.movePosition(QTextCursor::End);

    QTextCharFormat fmt = c.charFormat();
    QTextBlockFormat b = c.blockFormat();

    fmt.setFontWeight(QFont::Normal);
    fmt.setFontItalic(false);
    fmt.setFontUnderline(false);

    fmt.setAnchor(false);
    fmt.setAnchorHref(QString());
    fmt.setForeground(QBrush(Qt::black));

    b.setAlignment(Qt::AlignLeft);

    QFont f = fmt.font();
    f.setPixelSize(15);
    fmt.setFont(f);

    c.setCharFormat(fmt);

    c.setBlockFormat(b);

    c.insertText(text);

    c.movePosition(QTextCursor::End);

    doc->clearUndoRedoStacks();
}

int main(int argc, char **argv)
{
    QApplication app(argc, argv);

    QTextEdit e;
    fillTextDocument(e.document(), QStringLiteral("Hello\n\nHello"));
    e.resize(600, 400);
    e.show();

    return QApplication::exec();
}

Guys, I consider this as a bug of Qt 6.3.1, regression in comparison with Qt 5.15. With Qt 5.15 the problem is not reproducible.

0

There are 0 best solutions below