QSyntaxHighlighter with TextEdit, format is not reflecting in TextEdit qml.
-
//SearchHighlighter.cpp #ifndef SEARCHHIGHLIGHTER_H #define SEARCHHIGHLIGHTER_H #include <QSyntaxHighlighter> #include <QTextCharFormat> #include <QRegularExpression> #include <QQuickTextDocument> #include <QDebug> class SearchHighlighter : public QSyntaxHighlighter { Q_OBJECT public: SearchHighlighter(QTextDocument *parent = nullptr) : QSyntaxHighlighter(parent) {} Q_INVOKABLE void setSearchTerm(const QString &term){ searchTerm = term; rehighlight(); } Q_INVOKABLE void setDocObj(QQuickTextDocument* textDocObj) { qDebug() << textDocObj->textDocument(); setDocument(textDocObj->textDocument()); } protected: void highlightBlock(const QString &text) override { if (searchTerm.isEmpty()) return; QTextCharFormat fmt; fmt.setForeground(Qt::red); fmt.setBackground(Qt::yellow); QRegularExpression re(QRegularExpression::escape(searchTerm), QRegularExpression::CaseInsensitiveOption); QRegularExpressionMatchIterator i = re.globalMatch(text); while (i.hasNext()) { QRegularExpressionMatch match = i.next(); QSyntaxHighlighter::setFormat(match.capturedStart(), match.capturedLength(), fmt); } } private: QString searchTerm; }; #endif // SEARCHHIGHLIGHTER_H //main.cpp #include <QGuiApplication> #include <QQmlApplicationEngine> #include <searchhighlighter.h> #include <QTextDocument> #include <QQmlContext> int main(int argc, char *argv[]) { #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); #endif QGuiApplication app(argc, argv); QQmlApplicationEngine engine; const QUrl url(QStringLiteral("qrc:/main.qml")); QObject::connect( &engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); SearchHighlighter *hl = new SearchHighlighter(); engine.rootContext()->setContextProperty("searchHighlighter", hl); engine.load(url); return app.exec(); } //main.qml import QtQuick 2.15 import QtQuick.Controls 2.15 import QtQuick.Layouts 1.15 ApplicationWindow { visible: true width: 600 height: 400 ColumnLayout { anchors.fill: parent TextField { id: searchBox Layout.leftMargin: 10 Layout.topMargin: 10 placeholderText: "Search..." onTextChanged: searchHighlighter.setSearchTerm(text) } TextEdit { id: editor Layout.fillHeight: true Layout.fillWidth: true Layout.leftMargin: 10 font.pixelSize: 20 wrapMode: TextEdit.Wrap Component.onCompleted: { searchHighlighter.setDocObj(editor.textDocument) } } } }Once I enter text in search field (searchBox), corresponding text in TextFiled(editor) should highlight which would happen via SearchHighlighter classs.
But, for some reason, it is not highlighting. I checked with qDebug(), and everything is working fine within highlightBlock() function. But TextEdit remains same even after highlightBlock() function.Kindly assist and suggest if there is any other way to achieve this functionality.
-
Hi,
Which version of Qt are you using ?
On which OS ?
I tested your code on macOS with 6.7.2 and it's working properly.