当前位置:网站首页>Model and view of QT

Model and view of QT

2022-07-01 08:33:00 AlbertOS

introduce

Everyone does GUI Developed , Should understand the model / View architecture programming , But do you know its origin ?

Actually MVC In the framework of 1982 The year has been born since Small talk The framework of language , Now it has become a model of software design , Using a business logic 、 data 、 The interface displays the separated method organization code , Aggregation of business logic into one component , While improving and personalizing the interface and user interaction , No need to rewrite business logic .
MVC Is uniquely developed to map traditional input 、 Processing and output functions in a logical graphical user interface structure .

then MVC The frame is on fire ,Smalltalk But failed .
You may not have heard of Smalltalk, But it is actually recognized as the second object-oriented programming language in history ( The first is Simula67, I guess you haven't heard of it ) And the first truly integrated development environment (IDE), It has greatly promoted the production of many other design languages .
Yes Smalltalk I put the small knowledge of expansion at the end of my blog , You can read it as a popular science , Understand the origin of the next object

Model / View frame

  • Model (Model) It's the application object , Used to represent data ;
  • View (View) Is the user interface of the model , Used to display data ;
  • control (controller), Defines how the user interface responds to user input .

stay MVC Before , The user interface will 3 The two components come together ,MVC To separate them , This improves flexibility and reusability .
If you combine the two components of view and control , It forms a model / View frame . This also separates the storage of data from the presentation of data to users , But it provides a simpler framework .
Data and interface are separated . It makes it possible to display the same data in multiple different views , You can also create new views , Without changing the underlying data framework .
In order to deal with user input flexibly , Delegation is also introduced (Delegate) The concept of , It can be used to customize the rendering and editing of data .

 Insert picture description here
Each of these components uses an abstract class to define , It provides some general interfaces and default implementations of some functions ,
Model 、 View 、 Proxy components communicate with each other through signals and slots :

  • When the data of the data source changes , The model signals the view
  • When the user interacts with the displayed component , Views signal to provide interactive information
  • When editing an item , The delegate sends a signal , Tell the status of the model and view editor

Model

Qt All model classes of are based on QAbstractItemModel Virtual class , This defines the interface , Views and agents can access data .
Qt Some ready-made models are provided to deal with data items :

  • QStringListModel Used to store a simple QString A list of items ;
  • QStandardItemModel Manage complex tree structured data items , Each data item can contain any data ;
  • QFileSystemModel Provides information about files and directories in the local file system ;
  • QSqlQueryModel、QSqlTableModel and QSqlRelationalTableModel To access the database

If Qt These standard models provided cannot meet the needs , You can also subclass QAbstractItemModel、QAbstractListModel perhaps QAbstractTableModel To create a custom model .


In order to ensure that the representation of data is separated from the acquisition of data ,Qt The concept of model index is introduced .
Each piece of data that can be obtained through the model is represented by a model index , Both views and delegates use these indexes to request data items and display .
The model index has QModelIndex Class provides , It is a temporary reference to a piece of data , It can be used to retrieve or modify the data in the model .
Because the model can reorganize the internal structure at any time , In this way, the model index may fail , So you don't need and shouldn't store model indexes

View

Qt Several different types of views are provided :

  • QListView Display data items as a list
  • QTableView Displays the data in the model in a table
  • QTreeView Display the data items of the model in a hierarchical list .
  • These classes are based on QAbstractItemView Abstract base class , These classes can be used directly , It can also be subclassed to provide customized views .

Processing project selection

Model / The view architecture provides a very convenient method for selecting items . The information of the item selected in the view is stored in Q I t e m S e l e c t i o n M o d e l QItemSelectionModel QItemSelectionModel In the example , In this way, the model index of the selected item is kept in a separate model , Independent of all views . When setting up multiple models on one model , You can share choices among multiple views .
   The selection is specified by the selection range , Just record the model index at the beginning and end of each selection range , Discontinuous selection can be described using multiple selection ranges . Selection can be regarded as a set of model indexes saved in the selection model , The most recent project is called the current selection .

entrust ( agent )

In the model / In the view frame ,QAbstractItemDelegate Is the abstract base class of delegate ,
The default delegate implementation is implemented by QStyledItemDelegate Class provides , It's also called Qt Default delegate for standard views .
However ,QStyleItemDelegate and QItemDelegate It's independent of each other . Only one of them can be selected to draw and provide an editor for items in the view . The main difference between them is ,QStyledItemDelegate Use the current style to draw the project , therefore , When you want to implement a custom delegate, you need to Qt When applied with style sheets , It is recommended to use QStyleItemDelegate As the base class .

And MVC Different modes , Model / The view structure does not contain a complete separate component to deal with the interaction with users . General , The view is used to present the data in the model to the user , Also used to process user input .
   For greater flexibility , Interactions can be performed by delegates . These components provide input functions , It is also responsible for rendering individual items in some views . The standard interface for controlling delegation is QAbstractItemDelegate Definition in class .
   Delegation is achieved by painter() and sizeHint() Function so that they can render their own content . However , Simple component-based delegates can be subclassed QItemDelegate To achieve , Instead of using QAbstractItemDelegate, In this way, you can use the default items of these functions to implement . Delegated editors can be implemented in two ways , One is to use parts to manage the editing process , The other is to deal with events directly . You can refer to Qt Provided Spin Box Delegate Example Sample program to understand the first form . If you want to inherit QAbstractItemDelegate To achieve custom rendering operations , Then you can refer to Pixelator Example The sample program . in addition , You can also use QStyledItemDelegate As the base class , In this way, you can customize the display of data , This can be referred to as Star Delegate Example The sample program .
  Qt All standard views in use QItemDelegate To provide editing functions , The default implementation of this delegate interface is QListView、QTableView and QTreeView And other standard views provide a common style of rendering for each project . The default delegate in the standard view handles all standard roles , The specific content can be found in QItemDelegate Class . have access to itemDelegate() Function to get the delegate used in a view , Use setItemDelegate() Function can install a custom delegate for a view .


The origin of object orientation

When we talk about the origin, we have to talk about Simula67 This language , It first introduced the concept of object .
The earliest germination of the concept of object and instance in computer science can be traced back to MIT PDP-1 System . This system is probably the earliest capacity-based architecture (capability based architecture) The actual system . in addition 1963 year Ivan Sutherland Of Sketchpad The same idea is contained in the application .
Object as a programming entity dates back to 1960 Age from S i m u l a 67 Simula 67 Simula67 Language introduces thinking . S i m u l a Simula Simula The language is O l e − J o h a n D a h l Ole-Johan Dahl OleJohanDahl and K r i s t e n N y g a a r d Kristen Nygaard KristenNygaard Designed for simulating the environment at the Oslo computer center in Norway .

( It is said that , They designed this language to simulate ships , And he is interested in the interaction of different ship attributes . They grouped different ships into different classes , And every object , Based on its class , You can define its own properties and behavior .)

This method is the earliest concept of analytical procedure . In analytic programs , We map real-world objects to abstract objects , It's called “ simulation ”.Simula It's not just the introduction of “ class ” The concept of , The idea of example is also applied —— This may be the earliest application of these concepts .


20 century 70 Age Xerox P A R C PARC PARC Invented by the Institute S m a l l t a l k Smalltalk Smalltalk Language defines the concept of object-oriented programming as , In basic operations , Extensive use of objects and messages .

S m a l l t a l k Smalltalk Smalltalk The founder of was deeply S i m u l a 67 Simula 67 Simula67 The main ideological influence of , but Smalltalk Objects in are completely dynamic —— They can be created 、 Modify and destroy , This is related to S i m u l a Simula Simula Static objects in are different .
Besides , S m a l l t a l k Smalltalk Smalltalk The idea of inheritance is also introduced , Therefore, it goes beyond the programming model that cannot create instances and the model that does not have inheritance S i m u l a Simula Simula.

Feeling s m a l l t a l k smalltalk smalltalk The failure of is entirely an accident , because s u n sun sun The company refused to pay a high price for the installation s m a l l t a l k 80 smalltalk 80 smalltalk80 System , If at that time s u n sun sun If adopted s m a l l t a l k smalltalk smalltalk, Today's object-oriented programming is estimated to be s m a l l t a l k smalltalk smalltalk A series of

although s m a l l t a l k smalltalk smalltalk The original version of is not widely known , But its advantages and other variants are everywhere and some legacy technologies ,
First to s m a l l t a l k 80 smalltalk 80 smalltalk80 To write j i t jit jit The compiler brothers later wrote Google's v8 engine , This v8 Because of its good performance, the engine was later burned to the server and became n o d e j s nodejs nodejs At the heart of , There is a startup company s m a l l t a l k smalltalk smalltalk Compiler is s u n sun sun After acquisition , Become j a v a virtual quasi machine Of One Ministry branch ( h o t s p o t ) java Part of the virtual machine (hotspot) java virtual quasi machine Of One Ministry branch (hotspot),
The most famous is the windowed graphical user interface that Microsoft and apple have made their fortune , s m a l l t a l k smalltalk smalltalk The reason for the birth of the so-called object-oriented programming and graphical user interface , Even specialized just in time compilation techniques , Are derived from its own characteristics .
In the original language system , and C C C Language and u n i x unix unix The series is different , Its syntax is generated by the class itself , In a system that mixes data and code , The operating system is incorporated into the programming language , A class is responsible for a function , Is a programming language , A specific system ,
For example, in the initial system , No file system , No process control , Automatic memory recovery , Everything is a i m a g e image image The world , There are a lot of objects in it , When you develop a software , You can save it directly to someone else and open it on his disk , Or you can share it online “ Live ” Object to others , This object is the code after instantiation of a class , It is essentially a function , With specific input and output , As long as there is a host environment , Parse it out and generate it , The user gets this object , He can use this object to do the work he needs , If we put u n i x unix unix The system is compared to a country that sells factories , s m a l l t a l k smalltalk smalltalk It's like selling screwdrivers , You are free to choose the tools you need , Finish your work , The communication interface between these objects is completely consistent with the language standard , There is no need to encode and decode the communication between various systems .

But object - oriented languages are not a drawback , The biggest problem with objects is the confusion between data and logic , Programmers think that objects are the aggregation of logic and forget that objects are also the carrier of data .
The result is that a large number of programmers who do not understand the principles of the computer misuse objects to burst the memory , If the memory of the computer is only a few hundred MB Even a few dozen MB, Unmanaged programmers who have no concept of memory simply can't cover it .

So now the embedded system is still C The world of series , Still a paradise for experienced programmers , Learning the management of memory and the underlying principles of computer is not something that non majors can learn in a few months .
Many low code software and convenient development languages have lowered the development threshold , A person who doesn't know the code at all can develop a business system after several months' study , But the sophistication of the code is not something that these fast-paced programmers can harm .
It is no wonder that excellent design patterns and architectures are the successful experience of foreign coding , In China, there is basically no , Because excellent senior programmers are TM Changed careers !!!
Our current coding environment is too bad , Programmers are a job that needs to be accumulated over time , Now enterprises use newcomers who don't know the code at all ,35 How many senior programmers have been harmed by the job crisis at the age of ( They also show off that the composition of enterprises is young people ), Look at foreign programmers in their sixties and seventies everywhere , They are the pillars of the entire code world , We can't just focus on business - oriented code , Think TMD Rapid development is right ( Refactoring is troublesome , A piece of junk written in code ), We should make good code and be a good programmer !!!

When checking the data, I suddenly saw the thinking about the domestic coding environment , Is like , I'm sorry ~

原网站

版权声明
本文为[AlbertOS]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/182/202207010817572568.html