pyqt5 撤销QUndoGroup
时间: 2024-01-25 11:11:15 浏览: 52
以下是使用pyqt5实现撤销QUndoGroup的示例代码:
```python
from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, QUndoStack, QUndoGroup
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# 创建QUndoStack对象
self.undo_stack = QUndoStack(self)
# 创建QUndoGroup对象
self.undo_group = QUndoGroup(self)
# 将QUndoStack对象添加到QUndoGroup对象中
self.undo_group.addStack(self.undo_stack)
# 创建撤销QAction对象
undo_action = self.undo_group.createUndoAction(self, "Undo")
# 创建重做QAction对象
redo_action = self.undo_group.createRedoAction(self, "Redo")
# 将撤销和重做QAction对象添加到菜单栏中
edit_menu = self.menuBar().addMenu("Edit")
edit_menu.addAction(undo_action)
edit_menu.addAction(redo_action)
# 创建一个测试QAction对象,用于演示撤销和重做
test_action = QAction("Test", self)
test_action.triggered.connect(self.test)
# 将测试QAction对象添加到菜单栏中
file_menu = self.menuBar().addMenu("File")
file_menu.addAction(test_action)
def test(self):
# 创建一个测试QUndoCommand对象
command = TestCommand()
# 将测试QUndoCommand对象添加到QUndoStack对象中
self.undo_stack.push(command)
def undo(self):
# 调用QUndoGroup对象的undo()函数
self.undo_group.undo()
def redo(self):
# 调用QUndoGroup对象的redo()函数
self.undo_group.redo()
class TestCommand(QUndoCommand):
def __init__(self):
super().__init__()
def undo(self):
# 撤销操作
pass
def redo(self):
# 重做操作
pass
if __name__ == "__main__":
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)