View Classes

From https://doc.qt.io/qt-5/model-view-programming.html

Concepts

In the model/view architecture, the view obtains items of data from the model and presents them to the user. The way that the data is presented need not resemble the representation of the data provided by the model, and may be completely different from the underlying data structure used to store items of data.

The separation of content and presentation is achieved by the use of a standard model interface provided by QAbstractItemModel , a standard view interface provided by QAbstractItemView , and the use of model indexes that represent items of data in a general way. Views typically manage the overall layout of the data obtained from models. They may render individual items of data themselves, or use delegates to handle both rendering and editing features.

As well as presenting data, views handle navigation between items, and some aspects of item selection. The views also implement basic user interface features, such as context menus and drag and drop. A view can provide default editing facilities for items, or it may work with a delegate to provide a custom editor.

A view can be constructed without a model, but a model must be provided before it can display useful information. Views keep track of the items that the user has selected through the use of selections which can be maintained separately for each view, or shared between multiple views.

Using an existing view

Qt provides three ready-to-use view classes that present data from models in ways that are familiar to most users. QListView can display items from a model as a simple list, or in the form of a classic icon view. QTreeView displays items from a model as a hierarchy of lists, allowing deeply nested structures to be represented in a compact way. QTableView presents items from a model in the form of a table, much like the layout of a spreadsheet application.

image.png

The default behavior of the standard views shown above should be sufficient for most applications. They provide basic editing facilities, and can be customized to suit the needs of more specialized user interfaces.

Using a model

We take the string list model that we created as an example model , set it up with some data, and construct a view to display the contents of the model. This can all be performed within a single function.
Note that the StringListModel is declared as a QAbstractItemModel . This allows us to use the abstract interface to the model, and ensures that the code still works, even if we replace the string list model with a different model.

  • The list view provided by QListView is sufficient for presenting the items in the string list model. We construct the view, and set up the model using the following lines of code.
  • The view is shown in the normal way.
  • The view renders the contents of a model, accessing data via the model's interface. When the user tries to edit an item, the view uses a default delegate to provide an editor widget.
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QStringList numbers;
    numbers << "One" << "Two" << "Three" << "Four" << "Five";
    QAbstractItemModel *model = new StringListModel(numbers);
    QListView *view = new QListView;
    view->setModel(model);
    view->show();
    return app.exec();
}
image.png
  • The above image shows how a QListView represents the data in the string list model. Since the model is editable, the view automatically allows each item in the list to be edited using the default delegate.

你可能感兴趣的:(View Classes)