首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >QTextEdit中的撤消/重做

QTextEdit中的撤消/重做
EN

Stack Overflow用户
提问于 2019-04-14 14:47:52
回答 1查看 1K关注 0票数 1

我正在创建的程序包括一个QTextEdit部件。我想执行以下功能:

  1. 当我试图按下一个扮演bool撤销角色的QAction项时,当撤销的跟踪历史记录结束时,必须返回具体的值()。在成功执行该命令(上面的命令)之后,我猜还将实现另一个命令。
  2. 对于重做,必须执行相同的操作。

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-14 15:05:18

在Qt QTextEdit 文档中,您可以找到redoundo操作。此外,您还可以测试redoundo是否可以通过redoAvailableundoAvailable信号进行验证。

要实现这些操作,可以使用信号/时隙注册。

例如:

代码语言:javascript
运行
复制
#include <QVBoxLayout>
#include <QPushButton>
#include <QTextEdit>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    QPushButton *poUndo     = new QPushButton("Undo", this);
    QPushButton *poRedo     = new QPushButton("Redo", this);
    QTextEdit   *poTextEdit = new QTextEdit(this);
    QHBoxLayout *poHlayout  = new QHBoxLayout;

    QLabel * poLabelRedoAvaliable = new QLabel(this);
    QLabel * poLabelUndoAvaliable = new QLabel(this);


    // add undo/redo buttons
    poHlayout->addWidget(poRedo);
    poHlayout->addWidget(poUndo);

    QVBoxLayout *poVLayout  = new QVBoxLayout;
    poVLayout->addWidget(poTextEdit); // add text edit
    poVLayout->addLayout(poHlayout);

    // redo/undo avaliable status
    poVLayout->addWidget(poLabelRedoAvaliable);
    poVLayout->addWidget(poLabelUndoAvaliable);

    // main central widget
    QWidget *poCentral  = new QWidget(this);
    poCentral->setLayout(poVLayout);
    this->setCentralWidget(poCentral);

    // register the undo/redo actions actions
    connect(poUndo, &QPushButton::clicked,  poTextEdit, &QTextEdit::undo);
    connect(poRedo, &QPushButton::clicked,  poTextEdit, &QTextEdit::redo);

    connect(poTextEdit, &QTextEdit::redoAvailable,
            [poLabelRedoAvaliable](bool bAvailable)
    {
        if (bAvailable)
        {
            poLabelRedoAvaliable->setText("redo available");
        }
        else {
            poLabelRedoAvaliable->setText("redo not available");
        }
    });

    connect(poTextEdit, &QTextEdit::undoAvailable,
            [poLabelUndoAvaliable](bool bAvailable)
    {
        if (bAvailable)
        {
            poLabelUndoAvaliable->setText("undo available");
        }
        else {
            poLabelUndoAvaliable->setText("undo not available");
        }
    });

}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55676544

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档