gpt4 book ai didi

c++ - 在 QTextEdit 中撤消/重做

转载 作者:行者123 更新时间:2023-11-28 04:19:59 25 4
gpt4 key购买 nike

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

  1. 当我尝试按下一个 QAction 项目时,它起到了 Undo 的作用,然后当Undo轨迹历史结束时具体值(boolint)必须被退回。成功执行该命令(上述命令)后,我猜将执行另一个命令。

  2. Redo 必须执行相同的操作。

谢谢。

最佳答案

来自 Qt QTextEdit documentation ,您可以找到 redoundo 操作。您还可以通过 redoAvailableundoAvailable 信号测试 redoundo 是否可用。

要实现您可以使用信号/槽注册的操作。

例如:

#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");
}
});

}

关于c++ - 在 QTextEdit 中撤消/重做,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55676544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com