- 前言
- 一、QUndoCommand类
- 二、对文件读写进行撤销处理
- 总结
前言
开发Qt过程中需要实现撤销和重做功能,正好Qt有自带的QUndoCommand类
记录一下学习过程,如有问题可以指正讨论
从官方文档得知,Qt的撤销框架使用命令模式实现,具体什么是命令模式可以移步
Python设计模式之命令模式
一、QUndoCommand类
从官方文档得知,QUndoCommand是存储在撤消堆栈上的所有命令的基类。它可以应用(重做)或撤销文档中的单个更改。官方文档见
一个简单的撤销操作只需要其中的undo,redo,setText等操作
下面写一个学习过程中的例子来说明QUndoCommand类的使用
from PyQt5.QtWidgets import QUndoCommand, QUndoStack
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
self.item = []
class BuyItem(QUndoCommand):
def __init__(self, student, item):
super().__init__()
self.student = student
self.item = item
self.setText('购买' + item)
# super().__init__()
def redo(self) -> None:
print(self.student.name + '购买了' + self.item)
self.student.item.append(self.item)
def undo(self):
print(self.student.name + '退回了' + self.item)
self.student.item.pop()
def main():
undoStack = QUndoStack()
student1 = Student('xiaoming', '18')
undoStack.push(BuyItem(student1, 'phone'))
undoStack.push(BuyItem(student1, 'bag'))
undoStack.push(BuyItem(student1, 'pad'))
print('小明现在拥有', student1.item)
undoStack.undo()
print('小明现在拥有', student1.item)
if __name__ == '__main__':
main()
'''
xiaoming购买了phone
xiaoming购买了bag
xiaoming购买了pad
小明现在拥有 ['phone', 'bag', 'pad']
xiaoming退回了pad
小明现在拥有 ['phone', 'bag']
'''
可以看到需要进行的操作需要创建一个QUndoCommand类,每次执行对应的操作,都需要对该类进行实例化,其中初始化时的setText指定这条命令进行的操作。
redo函数实现具体的方法
undo函数是redo函数的撤回操作
使用撤回操作需要创建QUndoStack栈,将想要执行的操作push进栈中,同时执行一次redo函数
小明购买phone,bag,pad后,想退回pad,执行QUndoStack的undo函数可以撤销上一次操作。
代码如下:
import os
from PyQt5.QtWidgets import QUndoCommand, QUndoStack
class Write(QUndoCommand):
def __init__(self, filepath, text):
super(Write, self).__init__()
self.filepath = filepath
self.src = text
self.count = len(text)
self.size = os.path.getsize(filepath)
def redo(self) -> None:
with open(self.filepath, 'a+', encoding='utf-8') as input_file:
print('写入' + self.src)
input_file.write(self.src)
def undo(self) -> None:
with open(self.filepath, 'a+', encoding='utf-8') as input_file:
print('删除' + self.src)
input_file.truncate(self.size)
def main():
print(os.getcwd())
filepath = 'test.txt'
if not os.path.exists(filepath):
with open(filepath, 'w', encoding='utf-8') as a:
pass
with open(filepath, 'r', encoding='utf-8') as f:
print(f.read())
undoStack = QUndoStack()
undoStack.push(Write(filepath, '第一次输入n'))
with open(filepath, 'r', encoding='utf-8') as f:
print(f.read())
undoStack.push(Write(filepath, '第二十二次输入n'))
with open(filepath, 'r', encoding='utf-8') as f:
print(f.read())
undoStack.push(Write(filepath, '第三百三十三次输入n'))
with open(filepath, 'r', encoding='utf-8') as f:
print(f.read())
undoStack.undo()
with open(filepath, 'r', encoding='utf-8') as f:
print(f.read())
if __name__ == '__main__':
main()
'''
写入第一次输入
第一次输入
写入第二十二次输入
第一次输入
第二十二次输入
写入第三百三十三次输入
第一次输入
第二十二次输入
第三百三十三次输入
删除第三百三十三次输入
第一次输入
第二十二次输入
Process finished with exit code 0
'''
使用QUndoCommand完成了txt文件写入操作的撤销和重做。
总结 掌握了QUndoCommand的基本用法,其中redo和undo函数互为逆操作,还有QUndoView类没有展示,QUndoView类的用法是QUndoView(QundoStack),用于构建一个由undoText组成的List,具体的使用详见对文件读写进行撤销处理。


