Qt Undo/Redo 实现,无法连接到按钮

Qt Undo/Redo implementation, can’t connect to a button

我post问题也在Qt论坛,here

我正在尝试在我的应用程序中执行撤消和重做命令。我有一个 QTreeWidget,我想让用户撤消和重做一个操作(例如,更改 QTreeWidget 和 undo/redo 中的 QTreeWidgetItem 列中的值)。

这是我的部分代码:

class A.h

    class A : public QWidget
{
    Q_OBJECT
public:
    explicit A(...);
    ~A();

    ChangeValueTreeCommand      *commands;
    QUndoStack                  *undoStack;
    QPushButton                 *undoBtn;
    QPushButton                 *redoBtn;
    QString                      newValue;

    void changeItem(QTreeWidgetItem* treeWidgetItemChanged, int col);
};

class A.cpp

A::A(...){
undoStack = new QUndoStack(this);
}

void A::changeItem(QTreeWidgetItem* treeWidgetItemChanged, int col){

     ....
    commands = new ChangeValueTreeCommand(treeWidgetItemChanged, col, newValue);
    connect(undoBtn, SIGNAL(clicked()), commands, SLOT(undo()));
    undoStack->push(commands);

}

class Commands.h

    #ifndef COMMANDS_H
#define COMMANDS_H

#include <QApplication>

#include <QUndoCommand>
#include <QTreeWidgetItem>


class ChangeValueTreeCommand : public QUndoCommand
{
public:
    explicit ChangeValueTreeCommand(QTreeWidgetItem* treeWI = NULL, int c = 0, const QString changedV = "");
    ~ChangeValueTreeCommand();

    QTreeWidgetItem* treeWItem;
    const QString    changedValue;
    int col;

public slots:

    void undo();
    void redo();
};


#endif // COMMANDS_H

class Commands.cpp

    #include "Commands.h"

ChangeValueTreeCommand::ChangeValueTreeCommand(QTreeWidgetItem* treeWI, int c, const QString changedV)
        : treeWItem(treeWI), col(c), changedValue(changedV)

{}

ChangeValueTreeCommand::~ChangeValueTreeCommand(){}

void ChangeValueTreeCommand::undo()
{
    const QString oldValue = treeWItem->text(col);
    treeWItem->setText(col, oldValue);
}

void ChangeValueTreeCommand::redo()
{
    treeWItem->setText(col, changedValue);
}

问题是当用户改变QTreeWidgetItem中的值时,它会自动出现之前的值。此外,我想将撤消和重做功能连接到两个按钮,但编译器说

1543: error: C2664: ‘QMetaObject::Connection QObject::connect(const QObject *,const char *,const QObject *,const char *,Qt::ConnectionType)‘ÿ: cannot convert parameter 3 from ‘ChangeValueTreeCommand *’ to ‘const QObject *’ Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

有人可以帮助我吗?谢谢

您的 undoredo 按钮应该调用 undoStack->undo() / undoStack->redo()。这将移动堆栈指针并调用当前命令的 undo/redo 函数。

详见 Qt 文档:http://qt-project.org/doc/qt-4.8/qundostack.html#undo

特别是这部分:

New commands are pushed on the stack using push(). Commands can be undone and redone using undo() and redo(), or by triggering the actions returned by createUndoAction() and createRedoAction().

QUndoStack keeps track of the current command. This is the command which will be executed by the next call to redo(). The index of this command is returned by index(). The state of the edited object can be rolled forward or back using setIndex(). If the top-most command on the stack has already been redone, index() is equal to count().