1

I'm building a code editor with QPlainTextEdit. As default, when I type a bunch of words in one line and then press undo, the whole line gets deleted. I'd like to push to undo stack manually on every character, so that when I click undo, previous character is removed. Then I could implement capturing only non-letters, etc... so it would undo one logical token at a time. How do I do it?

I hope you guys can help me. Thanks in advance.

LogicStuff
  • 19,397
  • 6
  • 54
  • 74
  • For example you will paste 1000 characters, and do you want 1000 undo this? Maybe you should provide just one shortcut or button that will be delete last character or do some "smart" deleting instead of destroy all undo stack and fill it with the single character as you want. – Jablonski Oct 04 '14 at 10:52
  • For now, It would help me if you could show me how to push to undo stack on every space typed. Then, I'll figure out the rest. – LogicStuff Oct 04 '14 at 11:58

1 Answers1

0

I've got this working by overriding the keyPressEvent():

void CodeEditor::keyPressEvent(QKeyEvent *event)
{
    QTextCursor cursor = textCursor();
    cursor.beginEditBlock();
    QPlainTextEdit::keyPressEvent(event);
    cursor.endEditBlock();
    setTextCursor(cursor);
}
LogicStuff
  • 19,397
  • 6
  • 54
  • 74