Home > Net >  Modify a QTextDocument of a QML TextArea element
Modify a QTextDocument of a QML TextArea element

Time:09-27

I'm trying to create a minimal version of the following example: https://doc.qt.io/qt-5/qtquickcontrols-texteditor-example.html

I have created a DocumentHandler class with the following method:

class DocumentHandler : public QObject {
    Q_OBJECT
    QML_ELEMENT

public:
    Q_INVOKABLE void changeFormat(QQuickTextDocument *doc) {
        QTextCursor cursor(doc->textDocument());
        cursor.select(QTextCursor::Document);
        QTextCharFormat format;
        format.setFontItalic(true);
        cursor.mergeBlockCharFormat(format);
    }
}

Unfortunately, when I call this method from the following QML code

import DocumentTest 1.0
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Window 2.15

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Document Test")

    TextArea {
        id: textArea

        focus: true
        anchors.fill: parent
        wrapMode: Text.Wrap
        selectByMouse: true
        text: "Another day in paradise"
        onReleased: {
            document.changeChar(textDocument);
        }
        textFormat: Text.RichText
    }

    DocumentHandler {
        id: document
    }
}

I have a crash at the following line: https://github.com/qt/qtbase/blob/5.15.2/src/gui/text/qtextoption.h#L118

I read in the documentation that the QQuickTextDocument::textDocument() is read-only and that I can not modify its internal state so I wonder if using the QTextCursor is the right way to go. This is what is done in the example but I struggle finding out what is the difference with my code.

CodePudding user response:

Looking at https://doc.qt.io/qt-5/qml-qtquick-textedit.html#textFormat-prop, i would replace textFormat: Text.RichText with textFormat: TextEdit.RichText

CodePudding user response:

Sometime ago, I wrote a wrapper for the QSyntaxHighter classes. This achieves more than you require, but, it has the following elements:

  • control text color and formatting in your TextEdit
  • link to the TextEdit's textDocument property

Note that the example works with TextEdit not TextArea.

#include <QObject>
#include <QTextDocument>
#include <QSyntaxHighlighter>
#include <QQuickTextDocument>
class SyntaxHighlighter : public QSyntaxHighlighter
{
    Q_OBJECT
    Q_PROPERTY(QQuickTextDocument* textDocument READ textDocument WRITE setTextDocument NOTIFY textDocumentChanged)
public:
    SyntaxHighlighter(QObject* parent = nullptr);
    Q_INVOKABLE void setFormat(int start, int count, const QVariant& format);
    // ...
};

You can use it like this:

TextEdit {
    id: textEdit
}

SyntaxHighlighter {
    textDocument: textEdit.textDocument
    onHighlightBlock: {
        let rx = /[A-Za-z]/g;
        let m;
        while ( ( m = rx.exec(text) ) !== null ) {
            let keywords = [
                'import', 'function', 'bool', 'var',
                'int', 'string', 'let', 'const', 'property',
                'if', 'continue', 'for', 'break', 'while',
            ];
            if (keywords.includes(m[0])) {
                setFormat(m.index, m[0].length, keywordFormat);
                continue;
            }
        }
    }
}

TextCharFormat {
    id: keywordFormat
    foreground: "#808000"
    font.pointSize: 12
    font.bold: true; font.italic: true
}

For full source code, refer to:

  • Related