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.
Looking at https://doc.qt.io/qt-5/qml-qtquick-textedit.html#textFormat-prop, i would replace
textFormat: Text.RichTextwithtextFormat: TextEdit.RichText