The Model/view framework provides an abstract base class for model Qabstractitemmodel, which requires inheriting the class and implementing some necessary functions if a custom model is required.
In addition, QT provides Qabstracttablemodel and Qabstractlistmodel respectively inherit from the above base class, by the name can clearly know that these two classes apply to tabular model and list model respectively. For both of these models, many functions have been re-implemented, and they can be inherited directly when used.
Qabstractitemmodel
Qabstractitemmodel provides an abstract interface for the element model class, which needs to be inherited when customizing the model. The custom model is divided into a read-only model and an editable model.
Read-only model: internal data cannot be modified
In order to implement a read-only model, the following functions need to be re-implemented:
Flags
Qt::itemflags qabstractitemmodel::flags (const qmodelindex &index) const
This function returns a flag that is indexed to the model index and is used by other components to obtain information about each element. In many models, the returned flags should include qt::itemisenabled and qt::itemisselectable, which indicate that the elements in the model can be accessed and selected. The commonly implemented form is:
if (!index.isvalid ())
return qt::itemisenabled;
Return Qabstractitemmodel::flags (Index) | qt::itemisenabled | qt::itemisselectable;
Data
Qvariant Qabstractitemmodel::d ata (const qmodelindex &index, int role = Qt::D isplayrole) const
This function returns the underlying data for a model index, which includes information about an element, including rows, columns, and data, for the view and delegate access to the data. The usual implementation forms are as follows:
if (!index.isvalid ())
return qvariant ();
if (Index.row () >=/* Data Total number */)
return qvariant ();
if (role = = Qt::D isplayrole)
{
int row = Index.row ();
int column = Index.column ();
returns the data return/* data for the corresponding row and column according to the data structure used by the model to store it */
}
return Qvariant ();
Headerdata
qvariant qabstractitemmodel::headerdata (int section, qt::orientation Orientation, int role = = Qt::D isplayrole) const
This function returns the header of a section in the corresponding direction, providing an indication of the view at the top of the views (that is, the topmost and leftmost). The typical implementation form is:
if (Role! = Qt::D isplayrole)
return qvariant ();
if (orientation = = Qt::horizontal)//headersdata at the top, horizontal
or if (orientation = = qt::vertical)//headersdata in the leftmost, vertical direction
{
//Judge Section,section represents the first few
return ...
}
return Qvariant ();
RowCount
int Qabstractitemmodel::rowcount (const qmodelindex &parent = Qmodelindex ()) const
This function returns how many rows are in the given model index, and returns the number of children of the parent. Instead of the entire number of rows. If there are no child elements, 0 is returned.
For classes that inherit from Qabstractlistmodel, you only need to re-implement these four functions. For classes that inherit from Qabstracttablemodel and Qabstractitemmodel, the ColumnCount function needs to be re-implemented in addition to the four functions listed above, since the list has only 1 columns, and tables and trees need to be customized:
ColumnCount
int Qabstractitemmodel::columncount (const qmodelindex &parent = Qmodelindex ()) const
This function is usually unrelated to the given parent, and several columns are returned for the class involved.
Editable models: Allows data to be modified, or allows insertion and deletion operations.
Implementing an editable model, in addition to the functions of the above-mentioned read-only model, requires the following functions to be re-implemented:
Flags
Qt::itemflags qabstractitemmodel::flags (const qmodelindex &index) const
This function, like the function of a read-only model, simply returns with the addition of qt::itemiseditable, in the following form:
if (!index.isvalid ())
return qt::itemisenabled;
Return Qabstractitemmodel::flags (Index) | qt::itemiseditabled;
SetData
BOOL Qabstractitemmodel::setdata (const qmodelindex &index, const qvariant &value, int role = Qt::editrole)
The data function is used to get the SetData function, which sets the data stored in the model index, in the form of the following:
if (Index.isvalid () && role = = Editrole)
{
int row = Index.row ();
Locates from the internal data structure according to the line number and then adapts it to value
emit datachanged (index, index);
return true;
}
return false;
It is also important to note that after changing the internal data, it is necessary to issue datachanged (index, index) signals to inform views and delegate internal data has changed.
Setheaderdata
BOOL Qabstractitemmodel::setheaderdata (int section, qt::orientation Orientation, const qvariant &value, int role = Qt :: Editrole)
The Headerdata function is used to return a table header, which sets the header to value, in the following form:
if (role! = Editrole)
return false;
if (orientation = = Qt::horizontal) or if (orientation = = qt::vertical)
{
//change the storage table header variable
emit headerdatachanged ( Orientation, section, section);
return true;
}
return false;
Similarly, when a table header is changed, a headerdatachanged (Orientation, section, section) signal needs to be emitted, informing views and delegate.
If you need to add and delete operations, you need to implement the following functions:
insertrows
BOOL Qabstractitemmodel::insertrows (int position, int rows, const qmodelindex &index = Qmodelindex ())
This function inserts rows into rows in the position row of the structure in which the parent is index. The form is as follows:
Begininsertrows (index, position, position + rows-1);
Internal variable to delete operation
endinsertrows ();
return true;
The Begininsertrows () and endinsertrows () functions need to be called before and after the insert operation.
removerows
BOOL Qabstractitemmodel::removerows (int position, int rows, const qmodelindex &index = Qmodelindex ())
This function removes rows from row position in the structure of the parent index. The form is as follows:
Beginremoverows (index, position, position + rows-1);
Internal variable to delete operation
endremoverows ();
return true;
The Beginremoverows () and endremoverows () functions need to be called before and after the delete operation.
InsertColumns
Removecolumns
These two functions are similar to inserting rows and deleting rows, and you need to call two functions.
Normally, the function should return true if the operation succeeds, but if only a partial operation succeeds, such as inserting only a portion of the row, you need to return FALSE.