Important: Please read the Qt Code of Conduct -

Change in QTextLayout with Qt 6

  • Hello,

    I'm using in a project a QSyntaxHighlighter with some unit tests on my class, in which I manually set text and check the formats by using:

    const auto formats = textEdit.textCursor().block().layout()->formats();

    With Qt 6, the formats sometimes include the newline.

    -> To see the difference, I just added this code to the Syntax Highlighter example of Qt (in the MainWindow constructor):

    auto w = new QWidget;
    auto l = new QHBoxLayout;
    auto p = new QPushButton("Click");
    connect(p, &QPushButton::clicked, this, [this]() {
        editor->setPlainText("Here is /* a\n"
                             " comment in C++ */ !");
        // I check the second line
        const auto block = editor->textCursor().block().next();
        const auto formats = block.layout()->formats();
        qDebug() << "Block length:" << block.length() << "/" << block.text().count();
        qDebug() << "Format length:" << formats.first().start << "(" << formats.first().length << ")";

    And here are the results:

    Qt 5.15

    Block length: 12 / 11
    Format length: 0 ( 11 )

    Qt 6.0

    Block length: 12 / 11
    Format length: 0 ( 12 )

    It seems that Qt takes into account the newline (and not for the first line ?).
    Does anyone have an explanation ?

    Thanks a lot!!

  • Lifetime Qt Champion


    There's something not clear. You say that it sometimes include the carriage return. Is it something that happens only in the specific case you found out ?

  • Hi and thanks for the reply!

    Yes, it seems. For the first line I correctly get 4 for the length.

  • Maybe a bug with Qt 6 ?

  • Lifetime Qt Champion

    Sorry for the late reply, it looks like there's indeed something changed in between.

    You should check the release notes just in case it was mentioned somewhere. If nothing, then please consider opening a new report on the bug report system if there's not already something there.