-
Bug
-
Resolution: Duplicate
-
P1: Critical
-
None
-
6.0.0
-
None
-
Ubuntu 20.04
In Qt 6, sometimes carriage return characters are included in the length of QTextLayout formats.
With this code, we get different results in Qt 5.15 and Qt 6.0:
#include <QApplication> #include <QtWidgets> #include <QDebug> class Highlighter : public QSyntaxHighlighter { public: Highlighter(QTextDocument *parent = nullptr) : QSyntaxHighlighter(parent) { multiLineCommentFormat.setForeground(Qt::red); commentStartExpression = QRegularExpression(QStringLiteral("/\\*")); commentEndExpression = QRegularExpression(QStringLiteral("\\*/")); } protected: void highlightBlock(const QString &text) override { setCurrentBlockState(0); int startIndex = 0; if (previousBlockState() != 1) startIndex = text.indexOf(commentStartExpression); while (startIndex >= 0) { const QRegularExpressionMatch match = commentEndExpression.match(text, startIndex); const auto endIndex = match.capturedStart(); int commentLength = 0; if (endIndex == -1) { setCurrentBlockState(1); commentLength = text.length() - startIndex; } else { commentLength = endIndex - startIndex + match.capturedLength(); } setFormat(startIndex, commentLength, multiLineCommentFormat); startIndex = text.indexOf(commentStartExpression, startIndex + commentLength); } } private: QRegularExpression commentStartExpression; QRegularExpression commentEndExpression; QTextCharFormat multiLineCommentFormat; }; int main(int argc, char *argv[]) { QApplication app(argc, argv); QTextEdit editor; auto highlighter = new Highlighter(editor.document()); Q_UNUSED(highlighter); editor.setPlainText("Here is /* a\n" "multi-lines\n" "comment in C++ */ !"); const auto firstLine = editor.textCursor().block(); const auto format1 = firstLine.layout()->formats().first(); const auto secondLine = editor.textCursor().block().next(); const auto format2 = secondLine.layout()->formats().first(); qDebug() << "First line: From" << format1.start << "/" << format1.length << "chars"; qDebug() << "Second line: From" << format2.start << "/" << format2.length << "chars"; editor.show(); return app.exec(); }
Qt 5.15:
First line: From 8 / 4 chars Second line: From 0 / 11 chars
Qt 6.0:
First line: From 8 / 4 chars Second line: From 0 / 12 chars
- is replaced by
-
QTBUG-91042 REG Qt 6.0: QList/QVector::fill does not support shrinking list
-
- Closed
-