alexltr的个人主页

http://www.qtcn.org/bbs/u/107032  [收藏] [复制]

alexltr

我不是IT,只是喜欢Qt。 我不是程序员,只是与程序有缘。

  • 26

    关注

  • 60

    粉丝

  • 150

    访客

  • 等级:骑士
  • 身份:论坛版主
  • 总积分:537
  • 男,1976-01-01
  • 广州

最后登录:2024-04-20

更多资料

日志

Model/View Programming 模型与视图编程 -- <2>

2011-11-20 21:25
Using models and views
模型与视图的使用


The following sections explain how to use the model/view pattern in Qt. Each section includes an an example and is followed by a section showing how to create new components.
以下章节介绍在Qt中如何使用模型/视图模式。每一节都包含一个例子,紧接其后的一节是讲解如何创建新的组件。

Two models included in Qt
Qt中的两个模型

Two of the standard models provided by Qt are QStandardItemModel and QFileSystemModel. QStandardItemModel is a multi-purpose model that can be used to represent various different data structures needed by list, table, and tree views. This model also holds the items of data. QFileSystemModel is a model that maintains information about the contents of a directory. As a result, it does not hold any items of data itself, but simply represents files and directories on the local filing system.QFileSystemModel provides a ready-to-use model to experiment with, and can be easily configured to use existing data. Using this model, we can show how to set up a model for use with ready-made views, and explore how to manipulate data using model indexes.
Qt提供的两个标准模型分别是 QStandardItemModel 和QFileSystemModel. QStandardItemModel 是一个多用途模型,可以用来表现列表,表格,树型视图中所需的各种不同的数据结构。这个模型同时也包含数据项。QFileSystemModel是一个维护文件夹内容相关信息的模型。 因此,它本身并不包含任何数据项,只是简单的表示当前文件系统中的文件及文件夹.
QFileSystemModel 提供了一个现成的模型,只要进行简单的设置就可以使用现有的数据。通过使用这个模型,我们就可以知道如何建立一个模型来以使用现成的项视图,并探究如何使用模型索引来操控数据。


Using views with an existing model
现有模型与视图的使用

The QListView and QTreeView classes are the most suitable views to use with QFileSystemModel. The example presented below displays the contents of a directory in a tree view next to the same information in a list view. The views share the user's selection so that the selected items are highlighted in both views.
QListView 和 QTreeView 类是最适合QFileSystemModel模型使用的视图. 下面给出的例子显示了在一个树形视图中显示目录的内容,在旁边的列表视图中也显示相同的内容。两个视图共用一个用户选择,以便让两个视图中被选中的项都可以高亮显示。

We set up a QFileSystemModel so that it is ready for use, and create some views to display the contents of a directory. This shows the simplest way to use a model. The construction and use of the model is performed from within a single main() function:
我们先建立一个QFileSystemModel模型实例,然后创建一些视图用来显示目录的内容。它显示了使用模型的最简单的用法。模型的构造和使用都放在一个单独的main()函数内进行:
  1. int main(int argc, char *argv[])
    {
         QApplication app(argc, argv);
         QSplitter *splitter = new QSplitter;

         QFileSystemModel *model = new QFileSystemModel;
         model->setRootPath(QDir::currentPath());


The model is set up to use data from a certain file system. The call to setRootPath() tell the model which drive on the file system to expose to the views.
We create two views so that we can examine the items held in the model in two different ways:
建立这个模型是为了使用某个文件系统里的数据。setRootPath()函数的调用告诉模型文件系统中的那个磁盘的数据将显示在视图中。
我们建立两个视图,以便能够用两种不同的方法来检验模型中的项。

  1.      QTreeView *tree = new QTreeView(splitter);
         tree->setModel(model);
         tree->setRootIndex(model->index(QDir::currentPath()));
          QListView *list = new QListView(splitter);
         list->setModel(model);
         list->setRootIndex(model->index(QDir::currentPath()));


The views are constructed in the same way as other widgets. Setting up a view to display the items in the model is simply a matter of calling its setModel()function with the directory model as the argument. We filter the data supplied by the model by calling the setRootIndex() function on each view, passing a suitable model index from the file system model for the current directory.
构造视图的方法跟构造其它部件的方法一样。只要以目录模型作为参数来调用视图的setModel()函数,就可以建立起一个视图来显示模型里的项。通过调用每个视图上的setRootIndex()函数,并传递一个文件系统模型中的模型索引给它作为当前的目录,就可以对模型提供的数据进行筛选。

The index() function used in this case is unique to QFileSystemModel; we supply it with a directory and it returns a model index. Model indexes are discussed in Model Classes.
The rest of the function just displays the views within a splitter widget, and runs the application's event loop:
这个例子中使用的index()函数是QFileSystemModel类特有的,我们给它指定一个目录,然后它就返回一个模型索引。 模型索引将在Model Classes中进行讨论。
函数的其它语句只是将这些视图显示在一个分裂器中,并运行应用程序的事件循环:
  1.      splitter->setWindowTitle("Two views onto the same file system model");
         splitter->show();     return app.exec(); }

In the above example, we neglected to mention how to handle selections of items. This subject is covered in more detail in the section about Handling Selections in Item Views.
在以上的例子中,我们没有提及如何处理项的选择。这个主题将在“视图项选择的处理”这一章节中做详细的阐述。
分类:默认分类|回复:0|浏览:1505|全站可见|转载
 

Powered by phpwind v8.7 Certificate Copyright Time now is:05-09 16:10
©2005-2016 QTCN开发网 版权所有 Gzip disabled