gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:27:58 26 4
gpt4 key购买 nike

我也在 Qt 论坛中发布了这个问题,here

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

这是我的部分代码:

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

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);

}

类 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

类 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().

关于c++ - Qt Undo/Redo 实现,无法连接到按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28195457/

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