[][src]Struct qt_widgets::QUndoCommand

#[repr(C)]pub struct QUndoCommand { /* fields omitted */ }

The QUndoCommand class is the base class of all commands stored on a QUndoStack.

C++ class: QUndoCommand.

C++ documentation:

The QUndoCommand class is the base class of all commands stored on a QUndoStack.

For an overview of Qt's Undo Framework, see the overview document.

A QUndoCommand represents a single editing action on a document; for example, inserting or deleting a block of text in a text editor. QUndoCommand can apply a change to the document with redo() and undo the change with undo(). The implementations for these functions must be provided in a derived class.

class AppendText : public QUndoCommand { public: AppendText(QString doc, const QString &text) : m_document(doc), m_text(text) { setText("append text"); } virtual void undo() { m_document->chop(m_text.length()); } virtual void redo() { m_document->append(m_text); } private: QString m_document; QString m_text; };

A QUndoCommand has an associated text(). This is a short string describing what the command does. It is used to update the text properties of the stack's undo and redo actions; see QUndoStack::createUndoAction() and QUndoStack::createRedoAction().

QUndoCommand objects are owned by the stack they were pushed on. QUndoStack deletes a command if it has been undone and a new command is pushed. For example:

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

In effect, when a command is pushed, it becomes the top-most command on the stack.

To support command compression, QUndoCommand has an id() and the virtual function mergeWith(). These functions are used by QUndoStack::push().

To support command macros, a QUndoCommand object can have any number of child commands. Undoing or redoing the parent command will cause the child commands to be undone or redone. A command can be assigned to a parent explicitly in the constructor. In this case, the command will be owned by the parent.

The parent in this case is usually an empty command, in that it doesn't provide its own implementation of undo() and redo(). Instead, it uses the base implementations of these functions, which simply call undo() or redo() on all its children. The parent should, however, have a meaningful 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);

Another way to create macros is to use the convenience functions QUndoStack::beginMacro() and QUndoStack::endMacro().

Methods

impl QUndoCommand[src]

pub unsafe fn action_text(&self) -> CppBox<QString>[src]

Returns a short text string describing what this command does; for example, "insert text".

Calls C++ function: QString QUndoCommand::actionText() const.

C++ documentation:

Returns a short text string describing what this command does; for example, "insert text".

The text is used when the text properties of the stack's undo and redo actions are updated.

This function was introduced in Qt 4.8.

See also text(), setText(), QUndoStack::createUndoAction(), and QUndoStack::createRedoAction().

pub unsafe fn child(&self, index: c_int) -> Ptr<QUndoCommand>[src]

Returns the child command at index.

Calls C++ function: const QUndoCommand* QUndoCommand::child(int index) const.

C++ documentation:

Returns the child command at index.

This function was introduced in Qt 4.4.

See also childCount() and QUndoStack::command().

pub unsafe fn child_count(&self) -> c_int[src]

Returns the number of child commands in this command.

Calls C++ function: int QUndoCommand::childCount() const.

C++ documentation:

Returns the number of child commands in this command.

This function was introduced in Qt 4.4.

See also child().

pub unsafe fn id(&self) -> c_int[src]

Returns the ID of this command.

Calls C++ function: virtual int QUndoCommand::id() const.

C++ documentation:

Returns the ID of this command.

A command ID is used in command compression. It must be an integer unique to this command's class, or -1 if the command doesn't support compression.

If the command supports compression this function must be overridden in the derived class to return the correct ID. The base implementation returns -1.

QUndoStack::push() will only try to merge two commands if they have the same ID, and the ID is not -1.

See also mergeWith() and QUndoStack::push().

pub unsafe fn is_obsolete(&self) -> bool[src]

Returns whether the command is obsolete.

Calls C++ function: bool QUndoCommand::isObsolete() const.

C++ documentation:

Returns whether the command is obsolete.

The boolean is used for the automatic removal of commands that are not necessary in the stack anymore. The isObsolete function is checked in the functions QUndoStack::push(), QUndoStack::undo(), QUndoStack::redo(), and QUndoStack::setIndex().

This function was introduced in Qt 5.9.

See also setObsolete(), mergeWith(), QUndoStack::push(), QUndoStack::undo(), and QUndoStack::redo().

pub unsafe fn merge_with(&self, other: impl CastInto<Ptr<QUndoCommand>>) -> bool[src]

Attempts to merge this command with command. Returns true on success; otherwise returns false.

Calls C++ function: virtual bool QUndoCommand::mergeWith(const QUndoCommand* other).

C++ documentation:

Attempts to merge this command with command. Returns true on success; otherwise returns false.

If this function returns true, calling this command's redo() must have the same effect as redoing both this command and command. Similarly, calling this command's undo() must have the same effect as undoing command and this command.

QUndoStack will only try to merge two commands if they have the same id, and the id is not -1.

The default implementation returns 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; }

See also id() and QUndoStack::push().

pub unsafe fn from_q_undo_command(
    parent: impl CastInto<Ptr<QUndoCommand>>
) -> CppBox<QUndoCommand>
[src]

Constructs a QUndoCommand object with parent parent.

Calls C++ function: [constructor] void QUndoCommand::QUndoCommand(QUndoCommand* parent = …).

C++ documentation:

Constructs a QUndoCommand object with parent parent.

If parent is not 0, this command is appended to parent's child list. The parent command then owns this command and will delete it in its destructor.

See also ~QUndoCommand().

pub unsafe fn from_q_string_q_undo_command(
    text: impl CastInto<Ref<QString>>,
    parent: impl CastInto<Ptr<QUndoCommand>>
) -> CppBox<QUndoCommand>
[src]

Constructs a QUndoCommand object with the given parent and text.

Calls C++ function: [constructor] void QUndoCommand::QUndoCommand(const QString& text, QUndoCommand* parent = …).

C++ documentation:

Constructs a QUndoCommand object with the given parent and text.

If parent is not 0, this command is appended to parent's child list. The parent command then owns this command and will delete it in its destructor.

See also ~QUndoCommand().

pub unsafe fn new() -> CppBox<QUndoCommand>[src]

The QUndoCommand class is the base class of all commands stored on a QUndoStack.

Calls C++ function: [constructor] void QUndoCommand::QUndoCommand().

C++ documentation:

The QUndoCommand class is the base class of all commands stored on a QUndoStack.

For an overview of Qt's Undo Framework, see the overview document.

A QUndoCommand represents a single editing action on a document; for example, inserting or deleting a block of text in a text editor. QUndoCommand can apply a change to the document with redo() and undo the change with undo(). The implementations for these functions must be provided in a derived class.

class AppendText : public QUndoCommand { public: AppendText(QString doc, const QString &text) : m_document(doc), m_text(text) { setText("append text"); } virtual void undo() { m_document->chop(m_text.length()); } virtual void redo() { m_document->append(m_text); } private: QString m_document; QString m_text; };

A QUndoCommand has an associated text(). This is a short string describing what the command does. It is used to update the text properties of the stack's undo and redo actions; see QUndoStack::createUndoAction() and QUndoStack::createRedoAction().

QUndoCommand objects are owned by the stack they were pushed on. QUndoStack deletes a command if it has been undone and a new command is pushed. For example:

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

In effect, when a command is pushed, it becomes the top-most command on the stack.

To support command compression, QUndoCommand has an id() and the virtual function mergeWith(). These functions are used by QUndoStack::push().

To support command macros, a QUndoCommand object can have any number of child commands. Undoing or redoing the parent command will cause the child commands to be undone or redone. A command can be assigned to a parent explicitly in the constructor. In this case, the command will be owned by the parent.

The parent in this case is usually an empty command, in that it doesn't provide its own implementation of undo() and redo(). Instead, it uses the base implementations of these functions, which simply call undo() or redo() on all its children. The parent should, however, have a meaningful 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);

Another way to create macros is to use the convenience functions QUndoStack::beginMacro() and QUndoStack::endMacro().

pub unsafe fn from_q_string(
    text: impl CastInto<Ref<QString>>
) -> CppBox<QUndoCommand>
[src]

Constructs a QUndoCommand object with the given parent and text.

Calls C++ function: [constructor] void QUndoCommand::QUndoCommand(const QString& text).

C++ documentation:

Constructs a QUndoCommand object with the given parent and text.

If parent is not 0, this command is appended to parent's child list. The parent command then owns this command and will delete it in its destructor.

See also ~QUndoCommand().

pub unsafe fn redo(&self)[src]

Applies a change to the document. This function must be implemented in the derived class. Calling QUndoStack::push(), QUndoStack::undo() or QUndoStack::redo() from this function leads to undefined beahavior.

Calls C++ function: virtual void QUndoCommand::redo().

C++ documentation:

Applies a change to the document. This function must be implemented in the derived class. Calling QUndoStack::push(), QUndoStack::undo() or QUndoStack::redo() from this function leads to undefined beahavior.

The default implementation calls redo() on all child commands.

See also undo().

pub unsafe fn set_obsolete(&self, obsolete: bool)[src]

Sets whether the command is obsolete to obsolete.

Calls C++ function: void QUndoCommand::setObsolete(bool obsolete).

C++ documentation:

Sets whether the command is obsolete to obsolete.

This function was introduced in Qt 5.9.

See also isObsolete(), mergeWith(), QUndoStack::push(), QUndoStack::undo(), and QUndoStack::redo().

pub unsafe fn set_text(&self, text: impl CastInto<Ref<QString>>)[src]

Sets the command's text to be the text specified.

Calls C++ function: void QUndoCommand::setText(const QString& text).

C++ documentation:

Sets the command's text to be the text specified.

The specified text should be a short user-readable string describing what this command does.

If you need to have two different strings for text() and actionText(), separate them with "\n" and pass into this function. Even if you do not use this feature for English strings during development, you can still let translators use two different strings in order to match specific languages' needs. The described feature and the function actionText() are available since Qt 4.8.

See also text(), actionText(), QUndoStack::createUndoAction(), and QUndoStack::createRedoAction().

pub unsafe fn text(&self) -> CppBox<QString>[src]

Returns a short text string describing what this command does; for example, "insert text".

Calls C++ function: QString QUndoCommand::text() const.

C++ documentation:

Returns a short text string describing what this command does; for example, "insert text".

The text is used for names of items in QUndoView.

See also actionText(), setText(), QUndoStack::createUndoAction(), and QUndoStack::createRedoAction().

pub unsafe fn undo(&self)[src]

Reverts a change to the document. After undo() is called, the state of the document should be the same as before redo() was called. This function must be implemented in the derived class. Calling QUndoStack::push(), QUndoStack::undo() or QUndoStack::redo() from this function leads to undefined beahavior.

Calls C++ function: virtual void QUndoCommand::undo().

C++ documentation:

Reverts a change to the document. After undo() is called, the state of the document should be the same as before redo() was called. This function must be implemented in the derived class. Calling QUndoStack::push(), QUndoStack::undo() or QUndoStack::redo() from this function leads to undefined beahavior.

The default implementation calls undo() on all child commands in reverse order.

See also redo().

Trait Implementations

impl CppDeletable for QUndoCommand[src]

unsafe fn delete(&self)[src]

Destroys the QUndoCommand object and all child commands.

Calls C++ function: virtual [destructor] void QUndoCommand::~QUndoCommand().

C++ documentation:

Destroys the QUndoCommand object and all child commands.

See also QUndoCommand().

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T, U> CastInto<U> for T where
    U: CastFrom<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> StaticUpcast<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.