Qt Undo Framework Demo

Source: Internet
Author: User

Qt Undo Framework Demo

[Email protected]

Abstract. Qt's Undo Framework is a implementation of the Command Pattern, for implementing Undo/redo functionality in applications. The command pattern is based on the "idea", "all editing in an" application is done by creating instances of command Obje Cts. Command objects apply changes to the document and is stored on a command stack. Furthermore, each of the command knows how to undo the it changes to bring, the document, and its previous state. As long as the application only uses command objects-to-the state of the document, it's possible to undo a sequenc E of commands by traversing the stack downwards and calling undo on each command in turn. It is also possible to redo a sequence of commands by traversing the stack upwards and calling redo on each command.

Key Words. Qt, Undo/redo, Command Pattern, Model/view

1. Introduction

It is important to undo and redo (Undo/redo) capabilities in an interactive application. Like the common software Office,autocad, and so on, with the revocation function, the user experience more comfortable. Command mode is typically used to implement this function.

The command pattern enables a Toolbox object to request an unspecified application object by turning the request itself into an object that can be stored and passed like other objects. The key to this pattern is an abstract command class, which defines an interface for executing operations. Its simplest form is an abstract execute operation. The specific command subclass takes the receiver as an instance variable and implements an Execute operation that specifies the specific information that the receiver takes to execute the request. In Gof's "Design Patterns", the general structure of the command pattern is given, which is shown in 1.1:

Figure 1.1 Command Pattern structure

Encapsulates a request as an object so that you can parameterize the customer with different requests, queue or log requests, and support revocable operations.

The final step in supporting any level of undo and redo commands is to define a command history, or a list of commands that have been executed. Conceptually, the history of the command looks like the following shape:

Figure 1.2 Command History

Each circle represents a command object, and the object labeled present is the current command object. When we call Unexecute (), the object marked with present will move to the left, and when execute () is called, the object labeled present will move to the right. Repeating this process, we can do multiple levels of revocation, the number of levels is only limited by the length of the command history.

The following classes are mainly included in the undo framework for Qt:

V Qundocommand: This class is equivalent to that of the abstract base class command in the command pattern, all of which are saved to the undo stack and implemented in their derived classes with the undo and redo functions.

V Qundostack: This corresponds to the command history, which holds a list of commands objects.

V Qundogroup: is a combination of undo stacks.

V Qundoview: Is a list component that displays the contents of the undo stack, and clicking the name of the command in this view also implements the same function as the Undo/redo button.

This article is a simple example of the QT Undo framework, implemented in a simple list model, and then implemented on the tree. Master QT This framework, you can not Opencascade OCAF, and QT code is relatively simple and clear.

2.Example

QT provides an example of the undo framework, which also involves drawing related content, and the program effect is shown in 2.1:

Figure 2.1 Qt Undo Framework Example

In conjunction with this sample program, learn about the undo framework of Qt to write a simpler program, as shown in the following code:

ClassInsertCommand:PublicQundocommand
{
Public:
InsertCommand (ConstQmodelindex&Theindex, Qstringlistmodel* themodel);
    ~insertcommand ();

public:
    virtual void  undo ();
    virtual void redo ();

private:
     qmodelindex mindex;
    qstringlistmodel*  mModel;
};

First, the class InsertCommand that derives a plug-in string from Qundocommand, and to implement the two virtual functions of undo () and Redo (), the implementation code looks like this:

voidInsertcommand::undo ()
{
Mmodel->removerows (Mindex.row (),  1);
}

void insertcommand::redo ()
{
    mmodel->insertrows ( Mindex.row (),  1     mmodel->setdata (Mindex,  qstring ( "insert string  " +  Qstring::number (Mindex.row ()));
} /span>

In response to the function of the toolbar button, you only need to generate this command, and add the command to the command stack, the code is as follows:

void undotest::insertstring ()
{
Qmodelindex aindex = mlistview, Currentindex ();

Mundostack-push (new InsertCommand (Aindex, Mlistmodel));
}

The program works as shown in 2.2:

Figure 2.3 Test Qt Undo Framework

You can implement fallback and redo of commands by selecting them from the Undo/redo and Commands list on the toolbar. The complete program code can be downloaded via post link.

3.Conclusion

After learning C + + basic grammar, you can look at Gof's "design pattern". Just touch may feel a bit abstract, then you can use QT to write some programs to practice practiced hand. QT programming feels much more comfortable than MFC, and some classes are packaged in a straightforward and easy-to-use way. Although there is a document/view design pattern in MFC, MVC in QT is more straightforward. By using the existing framework to understand those abstract design patterns, it deepens the concept of object-oriented and makes your program simpler and more interesting.

Opencascade's OCAF framework also provides a data framework based on this tree-shaped framework that can store hierarchical representations of data, and also provides support for Undo/redo. Based on the OCAF framework, you can quickly develop a certain function of professional software. But to use the OCAF framework, there are many opencascade libraries involved. If you are going to develop a lightweight three-dimensional program and just choose QT to develop the GUI, consider using QT's MVC framework and the Undo/redo function on the framework, so that the development efficiency can be relatively high, and the program is published with a much smaller number of dynamic libraries to rely on.

The data frame in the popular factory design software is multi-tree, and the attributes on each node in the tree allow the user to expand freely, like OCAF by adding some integer attributes in the Tdatastd_integer, and adding the name attribute with Tdatastd_name. However, there are some limitations to adding attributes in OCAF, because each attribute is distinguished by a GUID, so there can only be one property on each node.

So using the QT MVC framework to implement a custom tree model as needed, and then implement a three-dimensional view based on v3d_viewer, you can achieve a simple, but seemingly relatively professional CAD modeling program.

4. References

1. GoF. Design patterns-elements of reusable object-oriented software. Machinery Industry Press. 2010

2. Qt5.4. Overview of the Qt ' s Undo Framework. 2014

3. Qt5.4. Undo Framework Example. 2014

4. OpenCASCADE6.8.0. OCAF. 2014

5. OpenCASCADE6.8.0. OCAF White Paper. 2014

6. OpenCASCADE6.8.0. Distribution of Data Through OCAF Tree. 2014

PDF Version and Source code:qt Undo Framework Demo

Qt Undo Framework Demo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.