Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QSyntaxHighlighter with TextEdit, format is not reflecting in TextEdit qml.
Forum Updated to NodeBB v4.3 + New Features

QSyntaxHighlighter with TextEdit, format is not reflecting in TextEdit qml.

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 554 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Thiru_16
    wrote on last edited by
    #1
    //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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved