• QT的QCommand的do和undo介绍


    QT的QCommand的介绍


    在Qt中,QCommand类是一个抽象类,它提供了redo()和undo()方法的纯虚函数,用于执行重做和撤销操作。QCommand类的目的是提供一种通用的方式来表示和执行命令式操作,这些操作可以是用户交互、程序逻辑或其他类型的操作。

    redo()方法用于执行重做操作,即撤销之前的撤销操作。它返回bool类型,表示操作是否成功执行。默认情况下,redo()方法返回false,表示无法执行重做操作。

    undo()方法用于执行撤销操作,即撤销之前的命令操作。它也返回bool类型,表示操作是否成功执行。默认情况下,undo()方法返回false,表示无法执行撤销操作。

    为了使用QCommand类,你需要创建一个继承自QCommand的具体子类,并实现redo()和undo()方法的实际逻辑。在你的子类中,你可以根据需要添加其他属性和方法来实现特定的命令操作。

    我们将使用QTextEdit作为文本编辑器,并使用QTextCursor来操作文本。

    首先,我们需要创建一个自定义的QCommand子类,用于实现do和undo操作。这个子类将包含一个QTextCursor对象,用于执行文本操作。

    #include   
    #include   
    #include   
      
    class TextCommand : public QCommand  
    {  
    public:  
        TextCommand(QTextEdit *textEdit, const QString &text, QTextCursor::MoveOperation operation, QUndoCommand *parent = nullptr)  
            : QCommand(parent), textEdit(textEdit), cursor(textEdit->textCursor()), text(text), operation(operation)  
        {  
            cursor.movePosition(QTextCursor::End);  
        }  
      
        void redo() override  
        {  
            cursor.movePosition(operation);  
            cursor.insertText(text);  
            textEdit->setTextCursor(cursor);  
        }  
      
        void undo() override  
        {  
            cursor.movePosition(operation);  
            cursor.select(QTextCursor::WordUnderCursor);  
            cursor.insertText(text);  
            textEdit->setTextCursor(cursor);  
        }  
      
    private:  
        QTextEdit *textEdit;  
        QTextCursor cursor;  
        QString text;  
        QTextCursor::MoveOperation operation;  
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    在上面的代码中,我们定义了一个名为TextCommand的类,它继承自QCommand。它有一个构造函数,用于初始化文本编辑器、文本和光标操作。在redo方法中,我们执行光标操作并插入文本,而在undo方法中,我们执行光标操作并选择当前单词,然后插入文本。最后,我们将光标设置回文本编辑器中。

    现在,我们可以在文本编辑器中使用TextCommand类来执行do和undo操作。以下是一个简单的示例:

    #include   
    #include   
    #include   
    #include   
    #include "textcommand.h"  
      
    int main(int argc, char *argv[])  
    {  
        QApplication app(argc, argv);  
      
        QTextEdit textEdit;  
        QUndoStack undoStack;  
        QPushButton undoButton("Undo");  
        QPushButton redoButton("Redo");  
      
        QObject::connect(&undoButton, &QPushButton::clicked, [&]() {  
            if (undoStack.canUndo()) {  
                undoStack.undo();  
            }  
        });  
      
        QObject::connect(&redoButton, &QPushButton::clicked, [&]() {  
            if (undoStack.canRedo()) {  
                undoStack.redo();  
            }  
        });  
      
        TextCommand *command = new TextCommand(&textEdit, "Hello", QTextCursor::NextWord);  
        undoStack.push(command);  
        command->redo();  
        command->undo();  
      
        textEdit.show();  
        undoButton.show();  
        redoButton.show();  
      
        return app.exec();  
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
  • 相关阅读:
    C语言数据的输入
    强迫症SuppressWarnings各场景的使用
    Docker(13)-- Docker 网络
    0X0-基于Sklearn的机器学习入门:聚类(上)
    通过Spring Boot 实现页面配置生成动态接口?
    【观察】ObjectScale:重新定义下一代对象存储,戴尔科技的重构与创新
    express-generator快速构建node后端项目
    配置Flutter开发环境
    【方案】基于视频与AI智能分析技术的城市轨道交通视频监控建设方案
    1759E(方案枚举)
  • 原文地址:https://blog.csdn.net/techenliu/article/details/133386743