1. View and Widgets
In the UI, the most common is list/grid/tree (in Qt, the grid is called table). In particular, for those database-related programs, it is possible to use a list or grid for each interface. In Qt, they are categorized as item view class. There are two implementations, one called item based, and these class names end with widgets, such as Qlistwidget. The other is called model based, and these classes end with a view, such as Qlistview.
The item based widget is very simple to use, similar to the Clistctrl/ctreectrl in MFC. These classes are both data container and display (presentation), as well as handling interactions with users (user action). But the disadvantage of using these classes is that they do not seem to work when dealing with large amounts of data. I remember using CListCtrl's report mode to show that more than 50,000 records would have to wait a long time. Because it's going to copy all the data and show it, for example, the list can only show 50 records at a time, but it also copies the remaining 49,950 entries into the memory, and adds the formatting and other information to each record's display. I think this implementation is unacceptable for professional software. So how to improve their performance? Some people come up with virtual List/grid. They do not need to load all the data before they appear, if there are 50,000 records, it shows the first 50 at the beginning and shows which one to pick. That is, after 49,950, it does not matter, when the user moves the scroll bar or the upper and lower keys to change the display, it calculates according to a series of parameters which currently need to be displayed. such as 1000-1050, then go to take these data display. Due to the implementation of a certain degree of difficulty, so a lot of MFC implementation of Virtual List/grid show is "Vegetarian Yan" (can quickly show it has been good). Of course, coding endless, this way of fetching data we call delayed fetch, if the implementation is not good, when the speed of the computer and the user quickly drag the scroll bar may also appear white screen, trailing, blurred and so on. So you can also do prefetch and caching to try to avoid these situations. Well, there are enough mats here, we are heading straight to the subject.
2, Model/view
pattern is MVC, so how does Qt come true? I opened the website introduction of QT (http://qt.nokia.com/doc/4.6/model-view-programming.html). I was dizzy, I saw dozens of classes, and many of them had inherited relationships. It's easy to lose people. But it doesn't matter, after 2 days of study, I made a summary of them, see. TX can start with my picture, grasp the main line of the framework, lest lost in the boundless tears (class) of the sea.
<a> Model
In general, the model does not really store data (the data can be stored directly in the model), its data is obtained from the real "meat (raw)", such as a disk file, or database query result set and so on. So what is this model for? To put it bluntly, it is responsible for acquiring and providing the "meat" data to the view, and then updating the view's changes to the "meat" data to the real "meat". So, a series of working with data, such as read-write files, operational databases, and network communications, is done in model. Sometimes "meat" may be really fat, so the model has an important job is to put these "meat" number. This is a very important class of Qmodelindex . In general, it uses a 2-D number (Row/colum) to number "meat". But for the hierarchical data of tree, a parent index is added as the 3rd number. That is, a father under the leaves are numbered from 0, 0, to obtain the model index when the return implementation. OK, now model has a bunch of "meat" of the number of numbers, who will buy ah? "Meat" is cheap ...
Model Data Indexing (index)
The items in model can be used as a variety of roles, which allows different data to be provided for different environments. For example, Qt::D isplayrole is used to access a string, which is displayed as text in view. Typically, each data item can provide data for many different roles, and the standard role is defined in Qt::itemdatarole. We can get the number we need by specifying the model index and the role :
Qvariant value = Model->data (index, role);
Model Item Role
The role indicates which type of data is referenced from the model. Views can display roles in different forms, so it is important to provide the correct information for each role. By providing appropriate data for each role, model also provides hints to views and delegates on how to correctly display these data items to the user. Different views are free to parse or ignore the data, and some additional roles can be defined for special occasions.
<b> View
The view appears in a timely manner, noting that many view can buy the same piece of meat at the same time. (Khan, no kidding, this blog fast water). When the view needs to display some data, they get the data from model by model index (call model's data function, and when the model data changes, it will automatically datachanged Signal to all the view so they can be updated). Of course, you can also call the model's setData function in view to set the data corresponding to a model index. Here to illustrate the model data, with qvarient to carry, can be all types of Qt support, more intimate is that the data can be divided into multiple roles (role), such as QT::D isplayrole dedicated to display, QT:: Backgroundrole is used to display the background color and so on. So in model, you can not only number "meat", but also "meat" for "deep processing" to make them more "good-looking" or more "delicious." The view organizes the data and displays it, but does not actually display the work, and the real work is left to the delegate.
<c> Delegate
Delegate is the controller in MVC. View lets it display when it is displayed in the Paint function. Of course, you can overload this function and implement your own display. You can also set the row delegate and Colum delegate for a view to be dedicated to row and Colum. when the user triggers the view's edit trigger (such as double-clicking the mouse or carriage return), the view begins with place edit (beginediting). Delegate will create a suitable widget (such as line edit or combo box) in the appropriate place to process the user's input, delegate get the user's input and return after the user input is completed. These inputs can be saved to the real "meat" by calling the model's SetData function. So delegate is actually responsible for the final display of data and the processing of user interaction.
Since the user interaction, the most important is definitely the user's choice. Say something about the selection model. View stores the item index selected by the user in the selection model, which is displayed according to the contents of the selection model. In addition, multiple view can share the same selection model, so that when you select one of them, the corresponding item in the other view is also selected.
3. Summary
Finally, I'm not sure how Smalltalk implemented MVC, but the QT Model/view Framework's implementation of MVC is based entirely on C + + 's virtual base class and virtual function features. In addition to the application of Signal/slot, the interconnection between the parts of MVC is all virtual functions. TX If you are interested, take a look at the classes that begin with qabstract in the Model/view Framework source code, which define a unified interface virtual function, only the derived classes need to re-implement the functions. Here are the 2 main lines that TX should focus on when studying this section:
(1), no matter what view, the role is to show the data, then the first line is how the data from the data source display to the view.
(2), the displayed data may be modified, so the second line is how the modified data will be updated to the data source. If you can draw these 2 sequence maps, I believe you've got it all figured out.
I don't know if Qt's design here is doing too much decoupling. Flexibility is enough anyway (model/view/delegate can inherit), but it can't get the programmer to grasp it quickly. Although Qt has weakened delegate and provided default delegate for view. However, in the case of specific projects, the programmer will most of the time to rewrite the number of derived classes according to the specific circumstances. This can be difficult for programmers who want to quickly interface and display. In short, this wheel is done fine, but generally before use you have to screw some screws, oil.
This article goes from: http://blog.csdn.net/leo115/article/details/7532682
This article refers to: http://blog.csdn.net/debugconsole/article/details/38307313
"Turn" Qt Mode/view