QT model / view programming using articles

Using models and views

  The following sections explain how to use the model / view mode in Qt. Each chapter includes an example of how then to create a new chapter components.

Qt contains two models

  Two standard models provided by Qt is QStandardItemModel and QFileSystemModel. QStandardItemModel is a versatile model can be used to represent a variety of different data structures required lists, tables and tree view. This model also contains data items. QFileSystemModel contents of the directory information is a model of maintenance. Therefore, it does not contain any data item, but only represent files and directories on the local filing system.

  QFileSystemModel a model can provide a ready test, and can easily be configured to use the existing data. Using this model, we can show you how to build a model to use ready-view and explore how to use the model index to manipulate data.

Use views on an existing model

  QListView and QTreeView view class is the most appropriate for use with QFileSystemModel. The following example shows the contents of the directory in the tree view, the same information is next in the list view. A view shared user's choice, to highlight the selected items in both views.

 

 

 We set QFileSystemModel so that you can use, and create some views to display the contents of a directory. This demonstrates the easiest way to use the model. The construction and use of models is a main () function is completed:

 

 

The model is to use data from a file system. To setRootPath () which drives on the system call tells the model file is open to view.

We created two views, so that we can project two different ways to check the model:

 

 

View configured in the same manner as other widgets. Set a view to display items in the model, the model simply use the directory as an argument to call it setModel () function. We call on each view by setRootIndex () function to filter the data provided by the model, transmission model index suitable for the current directory from the file system model.

The present embodiment used in index () function is unique to QFileSystemModel; we provide a directory for it, it returns a model index. Model discussed in class model index.

The rest of the splitter function show only a small part of the view, and the event loop to run the application:

 

 

In the above example, we ignore choose how to handle the project. Discuss this topic on the part of treatment options view in more detail.

Guess you like

Origin www.cnblogs.com/helloc14/p/12383293.html