1

I'm trying to create an Undo and Redo Button inside a GUI App using PyQt5 and Python 3.7.

When the Undo and Redo Buttons are clicked, the Key Sequences "Ctrl+Z" and "Ctrl+Y" should be executed respectively. Ive superficially gone through the documentation of QShortCut and QKeySequence but they seem to be designed for detecting key sequences and not triggering them. So how do I implement these buttons?

As per eyllanesc's comment, I'm adding this to better explain what I am trying to achieve.

self.undoButton = self.findChild(QtWidgets.QPushButton, 'undoButton')
self.undoButton.clicked.connect(self.undoButtonPressed)
self.anyPlainTextEdit = self.findChild(QtWidgets.QPlainTextEdit, 'anyPlainTextEdit')

# Function to Execute Key Sequence
def undoButtonPressed(self):
    # Execute Ctrl+Z Key Sequence

I'm wondering if this is even possible. If not, should I maintain Previous and current values of the PlainTextArea in separate variables and set the value of the PlainTextArea accordingly?

0

1 Answer 1

1

You don't have to launch the shortcut to enable redo or undo, just call the slot redo() and undo() when the buttons are pressed:

self.undoButton.clicked.connect(self.anyPlainTextEdit.undo)
self.redoButton.clicked.connect(self.anyPlainTextEdit.redo)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. I found this after you provided the solution: link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.