QUndoCommand Class
QUndoCommand 클래스는 QUndoStack...에 저장된 모든 명령의 기본 클래스입니다.. ..
헤더: | #include <QUndoCommand> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS Gui) target_link_libraries(mytarget PRIVATE Qt6::Gui) |
qmake: | QT += gui |
공용 함수
QUndoCommand(QUndoCommand *parent = nullptr) | |
QUndoCommand(const QString &text, QUndoCommand *parent = nullptr) | |
virtual | ~QUndoCommand() |
QString | actionText() const |
const QUndoCommand * | child(int index) const |
int | childCount() const |
virtual int | id() const |
bool | isObsolete() const |
virtual bool | mergeWith(const QUndoCommand *command) |
virtual void | redo() |
void | setObsolete(bool obsolete) |
void | setText(const QString &text) |
QString | text() const |
virtual void | undo() |
자세한 설명
Qt의 실행 취소 프레임워크에 대한 개요는 개요 문서를 참조하십시오.
QUndoCommand는 문서에 대한 단일 편집 작업(예: 텍스트 편집기에서 텍스트 블록을 삽입하거나 삭제하는 작업)을 나타냅니다. QUndoCommand는 redo()로 문서에 변경 사항을 적용하고 undo()로 변경 사항을 실행 취소할 수 있습니다. 이러한 함수에 대한 구현은 파생 클래스에서 제공되어야 합니다.
class AppendText : public QUndoCommand { public: AppendText(QString *doc, const QString &text) : m_document(doc), m_text(text) { setText("append text"); } void undo() override { m_document->chop(m_text.length()); } void redo() override { m_document->append(m_text); } private: QString *m_document; QString m_text; };
QUndoCommand에는 text()가 연관되어 있습니다. 이것은 명령이 수행하는 작업을 설명하는 짧은 문자열입니다. 스택의 실행 취소 및 다시 실행 작업의 텍스트 속성을 업데이트하는 데 사용됩니다( QUndoStack::createUndoAction() 및 QUndoStack::createRedoAction() 참조).
QUndoStack 은 명령이 실행 취소되고 새 명령이 푸시된 경우 해당 명령이 푸시된 스택이 소유합니다. 예를 들어
MyCommand *command1 = new MyCommand(); stack->push(command1); MyCommand *command2 = new MyCommand(); stack->push(command2); stack->undo(); MyCommand *command3 = new MyCommand(); stack->push(command3); // command2 gets deleted
실제로 명령이 푸시되면 해당 명령은 스택에서 가장 위에 있는 명령이 됩니다.
명령 압축을 지원하기 위해 QUndoCommand에는 id() 및 가상 함수 mergeWith()가 있습니다. 이 함수는 QUndoStack::push()에서 사용됩니다.
명령 매크로를 지원하기 위해 QUndoCommand 객체에는 자식 명령이 얼마든지 포함될 수 있습니다. 부모 명령을 실행 취소하거나 다시 실행하면 자식 명령이 실행 취소되거나 다시 실행됩니다. 생성자에서 명시적으로 부모에 명령을 할당할 수 있습니다. 이 경우 명령은 부모가 소유하게 됩니다.
이 경우 부모는 일반적으로 undo() 및 redo()의 자체 구현을 제공하지 않는다는 점에서 빈 명령입니다. 대신 모든 자식에서 undo() 또는 redo()를 호출하는 이러한 함수의 기본 구현을 사용합니다. 그러나 부모에는 의미 있는 text()가 있어야 합니다.
QUndoCommand *insertRed = new QUndoCommand(); // an empty command insertRed->setText("insert red text"); new InsertText(document, idx, text, insertRed); // becomes child of insertRed new SetColor(document, idx, text.length(), Qt::red, insertRed); stack.push(insertRed);
매크로를 만드는 또 다른 방법은 편의 함수 QUndoStack::beginMacro() 및 QUndoStack::endMacro()를 사용하는 것입니다.
QUndoStack 를참조하세요 .
멤버 함수 문서
[explicit]
QUndoCommand::QUndoCommand(QUndoCommand *parent = nullptr)
부모가 parent 인 QUndoCommand 객체를 생성합니다.
parent 이 nullptr
이 아닌 경우 이 명령은 부모의 자식 목록에 추가됩니다. 그러면 부모 명령이 이 명령을 소유하고 해당 소멸자에서 삭제합니다.
~QUndoCommand()도 참조하세요 .
[explicit]
QUndoCommand::QUndoCommand(const QString &text, QUndoCommand *parent = nullptr)
주어진 parent 및 text 을 사용하여 QUndoCommand 객체를 생성합니다.
parent 이 nullptr
이 아닌 경우 이 명령은 부모 명령의 자식 목록에 추가됩니다. 그러면 부모 명령이 이 명령을 소유하고 해당 소멸자에서 삭제합니다.
~QUndoCommand()도 참조하세요 .
[virtual noexcept]
QUndoCommand::~QUndoCommand()
QUndoCommand 객체와 모든 하위 명령을 삭제합니다.
QUndoCommand()도 참조하세요 .
QString QUndoCommand::actionText() const
이 명령이 수행하는 작업을 설명하는 짧은 텍스트 문자열(예: "텍스트 삽입")을 반환합니다.
이 텍스트는 스택의 실행 취소 및 다시 실행 작업의 텍스트 속성이 업데이트될 때 사용됩니다.
text(), setText(), QUndoStack::createUndoAction() 및 QUndoStack::createRedoAction()도 참조하세요 .
const QUndoCommand *QUndoCommand::child(int index) const
index 에서 하위 명령을 반환합니다.
childCount() 및 QUndoStack::command()도 참조하세요 .
int QUndoCommand::childCount() const
이 명령에 포함된 하위 명령의 수를 반환합니다.
child()도 참조하세요 .
[virtual]
int QUndoCommand::id() const
이 명령의 ID를 반환합니다.
명령 ID는 명령 압축에 사용됩니다. 이 명령의 클래스에 고유한 정수여야 하며, 명령이 압축을 지원하지 않는 경우 -1이어야 합니다.
명령이 압축을 지원하는 경우 올바른 ID를 반환하려면 파생 클래스에서 이 함수를 재정의해야 합니다. 기본 구현은 -1을 반환합니다.
QUndoStack::push()는 두 명령의 ID가 같고 ID가 -1이 아닌 경우에만 병합을 시도합니다.
mergeWith() 및 QUndoStack::push()도 참조하세요 .
bool QUndoCommand::isObsolete() const
명령이 더 이상 사용되지 않는지 여부를 반환합니다.
이 부울은 스택에서 더 이상 필요하지 않은 명령을 자동으로 제거하는 데 사용됩니다. QUndoStack::push (), QUndoStack::undo(), QUndoStack::redo(), QUndoStack::setIndex() 함수에서 isObsolete 함수를 확인할 수 있습니다.
setObsolete(), mergeWith(), QUndoStack::push(), QUndoStack::undo() 및 QUndoStack::redo()도 참조하세요 .
[virtual]
bool QUndoCommand::mergeWith(const QUndoCommand *command)
이 명령을 command 과 병합을 시도합니다. 성공하면 true
을 반환하고, 그렇지 않으면 false
을 반환합니다.
이 함수가 true
를 반환하는 경우 이 명령의 redo()을 호출하면 이 명령과 command 를 모두 다시 실행하는 것과 동일한 효과가 있어야 합니다. 마찬가지로 이 명령의 undo()를 호출하면 command 과 이 명령을 실행 취소하는 것과 동일한 효과가 있어야 합니다.
QUndoStack 는 두 명령의 ID가 같고 ID가 -1이 아닌 경우에만 병합을 시도합니다.
기본 구현은 false
을 반환합니다.
bool AppendText::mergeWith(const QUndoCommand *other) { if (other->id() != id()) // make sure other is also an AppendText command return false; m_text += static_cast<const AppendText*>(other)->m_text; return true; }
id() 및 QUndoStack::push()도 참조하세요 .
[virtual]
void QUndoCommand::redo()
문서에 변경 사항을 적용합니다. 이 함수는 파생 클래스에서 구현해야 합니다. 이 함수에서 QUndoStack::push(), QUndoStack::undo() 또는 QUndoStack::redo()를 호출하면 정의되지 않은 동작이 발생합니다.
기본 구현은 모든 하위 명령에서 redo()를 호출합니다.
undo()도 참조하세요 .
void QUndoCommand::setObsolete(bool obsolete)
명령이 더 이상 사용되지 않는지 여부를 obsolete 로 설정합니다.
isObsolete(), mergeWith(), QUndoStack::push(), QUndoStack::undo() 및 QUndoStack::redo()도 참조하세요 .
void QUndoCommand::setText(const QString &text)
명령의 텍스트를 지정된 text 로 설정합니다.
지정된 텍스트는 이 명령의 기능을 설명하는 사용자가 읽을 수 있는 짧은 문자열이어야 합니다.
text()와 actionText()에 대해 서로 다른 두 개의 문자열이 필요한 경우 "\n"으로 구분하여 이 함수에 전달하세요. 개발 중에 영어 문자열에 이 기능을 사용하지 않더라도 번역가가 특정 언어의 요구 사항에 맞게 두 개의 서로 다른 문자열을 사용하도록 할 수 있습니다. 설명한 기능과 actionText() 함수는 Qt 4.8부터 사용할 수 있습니다.
text(), actionText(), QUndoStack::createUndoAction() 및 QUndoStack::createRedoAction()도 참조하세요 .
QString QUndoCommand::text() const
이 명령이 수행하는 작업을 설명하는 짧은 텍스트 문자열을 반환합니다(예: "텍스트 삽입").
이 텍스트는 QUndoView 의 항목 이름에 사용됩니다.
actionText(), setText(), QUndoStack::createUndoAction() 및 QUndoStack::createRedoAction()도 참조하세요 .
[virtual]
void QUndoCommand::undo()
문서의 변경 내용을 되돌립니다. undo()가 호출된 후 문서의 상태는 redo()가 호출되기 전과 동일해야 합니다. 이 함수는 파생 클래스에서 구현해야 합니다. 이 함수에서 QUndoStack::push(), QUndoStack::undo() 또는 QUndoStack::redo()를 호출하면 정의되지 않은 동작이 발생합니다.
기본 구현은 모든 하위 명령에 대해 역순으로 undo()를 호출합니다.
redo()도 참조하세요 .
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.