MVC Tree Views

A WTreeView displays tree data (with additional properties available in extra columns). The archetypal application is a file system browser: the filesystem tree is displayed in the first column, and additional columns annotate properties of files.

As with other MVC item views, the actual data is provided by a WAbstractItemModel. This is a variation of the classical MVC strategy, where the View displays data provided by a Model. The View delegates the rendering of each individual item (i.e. table cell) to an Item Delegate to render (as a widget) the individual items, allowing this to be easily customized.

A more direct approach to showing a tree is to use the WTree or WTreeTable widgets which do not enforce this Model/View separation and allow you to directly specify the widgets that renders each cell. Compared to this, a WTreeView supports virtual scrolling (have you ever seen that for a tree ?), column sorting and resize handles, and row headers which allow you to scroll through many columns of additional data while keeping the first column within view. Its main restriction is that each row must have the same height.

The example below shows a tree view that is seeded with a custom hierarchical model which displays a revision in a git repository.

Example
Type
File
  • .classpath
    • .gitignore
      • .project
        • LICENSE
          • Markdown
            README.md
            • build.xml
              • Folder
                doc/
                • Folder
                  examples/
                  • jwt-4.10.4.pom
                    • jwt-auth-4.10.4.pom
                      • Folder
                        lib/
                        • overview.html
                          • Folder
                            src/
                            • Folder
                              test/
                              1 of 1

                              source
                                void TreeView() {
                                  WTreeView treeView = new WTreeView();
                                  treeView.resize(new WLength(600), new WLength(400));
                                  GitModel model = new GitModel("/path/to/repository/.git");
                                  treeView.setModel(model);
                                  treeView.setRowHeight(new WLength(24));
                                  treeView.setHeaderHeight(new WLength(24));
                                  treeView.setSortingEnabled(false);
                                  treeView.setSelectionMode(SelectionMode.Single);
                                  treeView.setEditTriggers(EnumSet.of(EditTrigger.None));
                                }
                              

                              Note The implementation of GitModel is discussed in Item Models

                              Other Features

                              The tree view shares a great deal of functionality with its sibling table view (which both reimplement WAbstractItemView), such as customizing cell rendering, editing and row headers. See MVC Table Views.

                              Top