Analysis of model/view framework of QT

Source: Internet
Author: User

Recently, I am reading the model/view of QT.
Framework, which is searched on the Internet. It seems that only a few Chinese translations are of little value. Article . Apart from the official introduction of QT, there are few other articles in E. You can see a foreigner in the blog
He wrote model/view as the worst part of QT. Is that true? To give back to open-source Community I wrote this blog and wrote something that I think is more valuable. Question
The analysis is required, but there are no special details. If you are interested, you can continue the discussion. I have read c ++ GUI programming with QT
4, second edition, QT official website, and QT Source code .

In the UI, list/GRID/tree is the most common one (in QT, grid is called table ). Especially for database-related Program , May be used on every interface
List or grid. In QT, they are classified as item View class. There are two implementations: one is item.
Based, these class names end with widgets, such as qlistwidget. Another type is model.
Based, these classes end with views, such as qlistview.
Item Based
Widgets are easy to use, similar to clistctrl/ctreectrl in MFC. These classes are both
Container), is also responsible for display (presentation), but also to process the interaction with the user (User
Action ). However, the disadvantage of using these classes is that it is insufficient to process a large amount of data display. I remember using clistctrl's report
It takes a long time for mode to display more than 50 thousand records. Because it needs to copy all the data and display it again, for example, this list can only display 50 records at a time, but it puts the remaining
49950 records are also copied to the memory, and additional information such as the format is added to the display of each record. 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 include all the data before it is displayed. If there are 50 thousand records, it will show the first 50 records at the beginning and the time to retrieve the records. That is to say, the last 49950
It does not matter. When you move the scroll bar or the up or down key to change the display content, it will calculate which items need to be displayed based on a series of parameters. For example, 1000-1050 records are returned. By
It is difficult to implement, so many virtual
LIST/grid display is "plain" (it is good to show it quickly ). Of course, there is no end to coding. This method of data retrieval is called delayed.
Fetch: if it is not easy to implement, when the computer is slow and the user is dragging the scroll bar quickly, there may also be white screen, tail, fuzzy, and other situations. So we can also do prefetch and caching.
Try your best to avoid these situations. Well, we have already paved the way here. Next we will go straight to the topic.
Model Based
View is what we need to focus on today's "resolution. The corresponding UI classes include qlistview, qtreeview, and qtableview. Then, these views correspond to
What is the difference between widgets? The name is the biggest difference. Widgets are originally referred to as xiaodongdong, while views are very "Atmospheric" views. How are these views displayed?
What about it? Let's talk about a design pattern that I don't want to talk about, but I have to say: MVC (model/View/controller), model stands for Data
Set), view stands for display, controller processes and interacts with the user
Action ). The MVC pattern is derived from smalltalk and seems to be used a lot now. The function of clistctrl is to free up the class mentioned above, because it
We have to do a lot of things, so we are too tired. Some programmers may not be able to implement a complex class. It is divided into three classes. Each class can better solve different problems. From Software Engineering
In this way, decoupling can reduce system risks. Well, if you want to know more about MVC, you can continue searching online.

The mode is MVC. How does QT implement it? I opened the QT official website introduction (http://qt.nokia.com/doc/4.6/model-view-
Programming.html ). I was dizzy and saw dozens of classes, and many of them had inheritance relationships. It's easy to get lost. But it doesn't matter. After two days of study, I summarized them,
See. TX users can start from my figure and grasp the main line of the Framework, so as not to get lost in the tears (class) Sea :-)




In general, the model does not actually store data (if there is little data, it can be directly stored in the model), and its data is obtained from the real "Raw, such as a disk
File or database query result
Set. So what is the purpose of this model? To put it bluntly, it is responsible for obtaining and providing the "meat" data to the view, and then modifying the "meat" data made by the view
New to the real "meat. Therefore, a series of data-related work such as reading and writing files, operating databases, and network communication is done in the model. Sometimes "meat" may be really fat, so
Another important task for model is to add these "meat" numbers. In this way, the model is displayed.
Index is a very important class. In general, it uses a 2-dimensional number (Row/colum) to number "meat. However, for hierarchical data such as tree, we add
The parent index is used as the 3rd number. That is, the leaves under a father are numbered from 0 to 0, and the model is obtained.
Use recursion to implement the index. OK. Now the model already has a bunch of "meat" for numbers. Who will buy them? "Meat" is cheap ......

View appears in due time. Note that many views can buy the same piece of meat at the same time ". (Khan, don't joke, this blog is almost watery ). When the view needs to display some data
The model index obtains data from the model (call the data function of the model. When the data of the model changes, it will also automatically send the datachanged
Signal to all views for updates ). Of course, you can also call the setdata function of the model to set a model in the view.
The data corresponding to the index. The data in the model is carried by qvarient. It can be of all types supported by QT. It is more considerate that data can be divided into multiple roles.
(Role), for example, QT: displayrole is used for display, QT: backgroundrole is used for displaying the background color, and so on. So in the model, you can
You can also perform "deep processing" on "meat" to make them "nice" or "delicious ". View organizes and displays the data, but does not really display the data.
For delegate.

Delegate is C in MVC. View displays it in the paint function. Of course, you can reload this function and implement your own display. You can also
View sets row delegate and Colum delegate for row and Colum. When the user triggers view Edit
Trigger (such as double-click the mouse or press Enter), view starts in place Edit
(Beginediting ). Delegate will create a suitable widget (such as line edit or combo) in a suitable place
(Box, etc.) process user input. After the user input is complete, delegate gets the user input and returns it. These inputs can be saved to the real
"Meat. Therefore, delegate is responsible for final data display and user interaction processing.
Since user interaction is the most important thing is the user's choice. Let's talk about selection model. View
All indexes are stored in the selection model.
The content of the model is displayed. In addition, multiple views can share the same selection.
Model, so that when you select one of them, the corresponding item in the other view will also be selected.

Finally, I don't know how Smalltalk implements MVC, but QT model/View
The implementation of the framework for MVC is completely based on the virtual base class and virtual function features of C ++. In addition to applying signal/slot, the interconnection between various MVC parts is all virtual functions.
If you are interested, you can check the model/view.
Framework SourceCodeClass whose names start with qabstract. After they define uniform interface virtual functions, the derived class only needs to implement these functions again. I will repeat this article to learn about it.
The two main lines that time-sharing TX should pay attention.
No matter what view is used to display the data, the first main line is how the data is displayed from the data source to the view.
The displayed data may be modified, so the second main line is how the modified data can be updated to the data source. If you can draw the two sequence maps, I believe you have fully understood them.


I don't know if the design of QT here is too much decoupling. Flexibility is sufficient (model/View/delegate can be inherited), but the process cannot be
The sequencer can quickly master the problem. Although QT has weakened the delegate and provided default delegate for the view. However, when working on a specific project, programmers usually need
Override the number of derived classes. This may be unacceptable for programmers who want to quickly create and Display Interfaces. In short, this wheel is fine enough, but before you use it, you need to screw it yourself and oil it.

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.