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.
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 好声音的舞台火了马啪啪的《南山南》 ,让一群只知道“爱上一匹野马,可我的家里没有草原”的人开始知道了“頔”这个字念...
    沈奕阅读 4,702评论 1 1
  • 有时候莫名其妙的心烦,不想和任何人说话,有时候只是因为一些小事,却能开心半天,大家都说,成熟就要学会控制自己的情绪...
    Etched阅读 4,821评论 0 1
  • 相信大家都有过抱怨的时候,或许是因为学校食堂饭菜不好吃,或许是因为学习考试太频繁,或许是因为爸妈的唠唠叨叨.....
    南湘072孟茹阅读 2,675评论 0 2