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

Add irreversible changes in a QPlainTextEdit



  • Hello !

    I'm working on a kind of code editor. To dot it, I'm using a QPlainTextEdit, as this example shows, to be able to display the line numbers for example.
    But I also want to add some anchors and html links in this editor to jump from some lines to others.
    I know that for this, QTextBrowser is more suitable, but I want to keep my editor features.

    And when I add a link with a QPlainTextEdit, I don't want to add an item in the undo stack. So I tried something like this :

    auto editor = new QPlainTextEdit;
    
    void addLink()
    {
        editor->setUndoRedoEnabled(false);
        editor->document()->blockSignals(true);   // To prevent the modificationChanged to be emitted.
    
        QTextCursor cursor { editor->textCursor() };
        cursor.insertHtml("<a href=\"anchor\">LINK</a>");    // Actually we replace the existing text with a link.
    
        editor->document()->blockSignals(false);
        editor->setUndoRedoEnabled(true);
    }
    

    But the problem is that setUndoRedoEnabled clears the undo/redo stack. We should use QSyntaxHighlighter to avoid adding an item in the stack but is it possible to add html link ??

    • Actually, here are the three options I see :

    • Be able to add some text without changing the undo/redo stack
    • Be able to add html links with the highlighter
    • Be able to display the line numbers using a QTextBrowser

    Maybe there is another solution.
    Thanks for your help !


  • Lifetime Qt Champion

    Hi,

    AFAIK you cannot do it easily.

    Note that QTextBrowser can be made writable.