Friday, January 23, 2015

PySide Tree Tutorial IIA: An introduction to simpletreemodel

Part of a series on treebuilding in PySide: see Table of Contents

The simpletreemodel example that comes with PySide includes the following files:
simpletreemodel.py     #the main program
simpletreemodel_rc.py  #compiled resource file
default.txt            #text file of data in GUI
 The GUI displays a tree view of the Table of Contents of a Qt Designer tutorial (Figure 3). It provides basic keyboard navigation that is the default in all tree views (e.g., pressing the right arrow expands an item to show its children). Note that each row in the view contains two columns of data: a title (e.g.,'Getting Started') and a summary (e.g., 'How to familiarize yourself with Qt Designer').

Figure 3: The GUI created by the application

There are two main classes defined in simpletreemodel.py:
  1. A home-grown TreeItem class that represents individual rows in the tree. Our data store consists of multiple TreeItem instances connected into a hierarchically organized tree.
  2. A TreeModel class, subclassed from QAbstractItemModel, that serves as a wrapper for the data structure built out of TreeItems. It implements the API needed by the view (as discussed in post IA).
Parts II and III of this series will focus on these two classes, respectively.

Before moving on in the tutorial, I strongly recommend that you check to make sure that simpletreemodel runs as expected on your system. If it does not, then feel free to ask for help in the comments.

4 comments:

Anonymous said...

Do you know how to get simpletreemodel_rc.py from default.txt and simpletreemodel.qrc?
Thanks!

Anonymous said...

Really helpful.

Eric Thomson said...

Anonymous: yes that topic is not covered very often.

To compile the resource file (let's call it resource.qrc) use something like the following command at your system prompt, with the containing folder as your working directory (let's assume you want it to be compiled/saved as resource_rc.py):

pyside-rcc resources.qrc -o resource_rc.py

This assumes the folder containing pyside-rcc.exe is on your system path (you may have to add it yourself).

Eric Thomson said...

Anon, this is also covered this some in Pyside Summer chapter 6 of Summerfield's book.