Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Add irreversible changes in a QPlainTextEdit
QtWS25 Last Chance

Add irreversible changes in a QPlainTextEdit

Scheduled Pinned Locked Moved Unsolved General and Desktop
qplaintextedithighlight
2 Posts 2 Posters 365 Views
  • 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.
  • M Offline
    M Offline
    Maluna34
    wrote on 7 Sept 2020, 14:12 last edited by Maluna34 9 Jul 2020, 14:33
    #1

    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 !

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 7 Sept 2020, 17:40 last edited by
      #2

      Hi,

      AFAIK you cannot do it easily.

      Note that QTextBrowser can be made writable.

      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

      1/2

      7 Sept 2020, 14:12

      • Login

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