The Model-View-Controller (MVC) arquitecture manages the relation between data and forms that are being drawn in a widget (usually in table and list morphics). NVM consists principally of 3 components.
1. The Model, it is a application object that can be the source or gateway of the data. The base class of every model in Qt is QAbstractItemModel.
2. The View, it is the presentation in the display of the data. The base class of every view in Qt is QAbstractItemView.
3.The Delegate defines the way that the software react to the interaction of the user with the displayed data. The base class of every delegate is QAbstractItemDelegate.
The advantages of using the MVC architecture instead of combined model-view objects (QTreeWidget, QListWidget, and QTableWidget) are:
- The presentation of Widgets are more flexible, permiting the customization of development and interaction with the user.
- The model interface that permits a better range of data types to be displayed in the views.
The best way to access the information of a model is to use QModelIndex indexes. An index can contain different data types (images, text, tooltips, and so on) and by using the Roles, we can tell the view how to represent visually the data from the index.
To support the different types of data, QVariant is used. QVariant is an abstract object that can store types in-built as well as QObject derived types (also types declared with QMetaType).
All Qt model uses the Signal & Slots to inform the changes in their data source. Moreover, a model is represented as a information table to ease the index handling. For hierarquical models, such as the tree model, the indexes contain a reference to their father to maintain a father-child link.
The principal Qt views are: QTableView, QTreeView, QColumnView y QListView, even that, those can be derivated to have a better customized view.
The delegate is the “bridge” between the veiw and the model. Its principal proposite is to manage the editions of index that are made by the user. The flexibility of the delegate permits multiple methods to insert values, for example, when the user wish to change the value of the view, a dialog or a QLineEdit or a QWidget can show up.
Even more, of the previosly described components, the MVC arquitecture of Qt implements secondary classes such as:
1. Specialized QAbstractItemModel and QAbstractItemView that supports the functionality of Drag and Drop.
2. QSortFilterProxyModel to make sorting and filters of the displayed data in the view.
3. QItemSelectionModel manages the selection methods of single and multiple indexes.
As an example of the use of MVC architecture, there is a table implementation of components of the car by a QTableView (beware the text is in Spanish – no useful meaning for this topic).
In this case, the model is composed by a list of “MechItem”, objects that represent the data that it is desired to show in the view. A class is being created from QAbstractTableModel and the following functions are implemented.
- rowCount() y columnCount() maintain the model size.
- headerData() specifies how many and how header of the table will be displayed.
- data() returns the respective data in the function fo the model index.
- For models that can be edited, flags() and setData() function are implemented, these functions indicates to the model how to handle the edited data of the delegate.
The data type ddetermines the role type, for example a QString is intepreted by the rol of Qt::DisplayRole, while a QImage is intepreted as Qt::DecorativeRole.
In data() , the role use is displayed to determine how to use the data:
headerData() only shows the text:
The flags() methos first verify if the index is still valid and returns Qt::ItemIsEnable and Qt::ItemIsEditable, that permits the ability to select and edit with a QEditLine each celd.
The setData() method validates the index position to continue with the data edition of an MechItem’s container. In the end, emit a signal with the modified index.
When there is a double click in the cled, the selected index can be edited.