Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Read/Write WYSIWYG RichText Editor with QML



  • having read and tried out the Qt Quick TextEditor example, I am trying to understand what the best practices are, when trying to write a QML-Based application that changes the underlying TextDocument of a TextEdit (the QML one).

    Specifically the modifications that are being done through the C++ TextCursor interface e.g. in:

    void DocumentHandler::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
    {
        QTextCursor cursor = textCursor();
        if (!cursor.hasSelection())
            cursor.select(QTextCursor::WordUnderCursor);
        cursor.mergeCharFormat(format);
    }
    

    seem to contradict the API documentation of QQuickTextDocument, explicitly stating:

    Warning: The QTextDocument provided is used internally by Qt Quick elements to provide text manipulation primitives. You are not allowed to perform any modification of the internal state of the QTextDocument. If you do, the element in question may stop functioning or crash.

    https://doc.qt.io/qt-5/qquicktextdocument.html

    The changes performed by user interaction (toggling bold, italic, specifying a link) all change the internal state of the QTextDocument, at least the produce or remove new QTextFragments.

    Is a change in formatting not considered a modification of the internal state?

    Apart from that, I only found examples of read and write Text Editor based on a QtQuick-UI that do manually string replacements to add HTML around the current selection. Is this really the way to go?

    Maybe this is just me being too cautious before continuing where the Text Editor C++ example stops...


Log in to reply