QT Models/views, delegates

Source: Internet
Author: User

The combination of the MVC view and the Controller object results in a model/view structure that still separates the data from the way it is presented to the user, making it possible to display the same data in several different views and implement a new type of view without altering the underlying data structure. In order to handle data input flexibly, the concept of delegation is introduced. There's something about introducing a delegate in this framework: Allow project data display and custom edits.

The model communicates with the data source, which provides an interface to other components in the architecture. The nature of communication depends on the type of data source and how the model is implemented.

Views get model indexes from the model, which are references to data items. By providing a model index to the model, the view can retrieve data items from the data far away.

In the standard view, the delegate renders the data item. When an item is edited, the delegate and the model communicate directly using the model index.

Models, views, delegates use signals and slots to communicate with each other

Model

All models are based on the Qabstractitemmodel class. This class defines an interface that uses views and delegates to access data, Qabstractlistmodel and Qabstracttablemodel

QT provides a number of ready-made models that can be used to manipulate data items:

Qstringlistmodel,qstandarditemmodel,qfilesystemmodel,

View

Based on Qabstractitemview abstract base class, Qlistview,qtableview,qtreeview

Commissioned

Qabstractitemdelegate represents the base class of abstraction in the model/view framework. The default delegate is provided by Qstyleditemdelegate, which is used by the standard view of QT as the default delegate. However, Qstyleditemdelegate and Qitemdelegate are independent of the painting and provide an editor for the view. The difference between them is that Qstyleditemdelegate uses the current style to draw the project.

Model indexes

Views and delegates request the displayed data items through the model index, the model only needs to know how to get the data, and the type of data managed by the model can be fairly universally defined.

The temporary model index is provided by the Qmodelindex class, and the persistence Model index is provided by the Qpersistentmodeindex class, which gets the model index corresponding to the data item, and the model must have three attributes: a line number, a column number, and the model index of the parent item

Project role

Projects in the model can deduce different roles for other components, allowing different types of data to be provided for different situations. Typically, items that contain data are used in several different roles, and standard roles are qt::itemdatarole defined

We can request project data to the model through a model index that is passed to the project, and get the desired data type by specifying a role

The data type is called the role indicator of the model. Views can display roles in different ways, so it is important to provide the appropriate information for each role.

Commissioned

The standard interface for control delegation is defined in the Qabstractitemdelegate class, and the standard view provided by QT uses Qitemdelegate. The default implementation of the delegate interface renders the project as a consistent style for each standard view: Qlistview, Qtableview, Qtreeview.

All standard roles are handled by the default delegate in the standard view that is used.

QT Models/views, delegates

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.