Creating Custom Hierarchy with QTreeView/QAbstractItemModel

  • Hey all,

    I've started using Qt, and this is my first post here! I'm working on an OpenGL-based animation application for signed-distance field fonts, and I need to create a hierarchy model that's got 3 levels to it: font, strings and animations. The top level represents a list of loaded type faces. Each type face item has its own list of "strings" (that's the mid-level to my hierarchy). Each "string" has a list of "animations" to it (that's the 3rd and bottom level). This isn't so much like a scene graph hierarchy where nodes' parent/child relationships can change --it's more like Qt Creator's own Project dock's hierarchy with projects at the top level with folder filters beneath them followed by files.

    Do I have to make 3 QAbstractItemModel subclasses for my Fonts, Strings and Animations? Also, do the model(s) responsible for holding the data needed for my Fonts, Strings and Animations, or do I have separate classes for that, then feed that to my model? I've been going through the docs for QTreeView and QAbstractItemModel. On top of that, I've found a few things regarding custom implementations, like this tutorial here.

    Also, what is the point of QAbstractItemModel's "role"? From what I've gathered, one of the roles in can have is "Display", which should be used to represent data that's to be displayed to the user. I'm not too sure what other roles there are, and how Qt enforces them outside of explicit logic that's up to the programmer when making the subclass.

    Also, are there any good resources that have different examples on how to create custom views for QTreeView?

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    The other roles are used already by the views e.g. the decoration role to show an icon, the edit role to get the data to edit (matches usually the display role but might be different) etc. It's indeed the developer roles to provide useful data for the roles that make sense for its model, other wise either return an invalid QVariant or the base class value.

    You also have the Simple Tree Model example to help you get started.

    If you already have a data structure for your Font/String/Animation combo, you can use QAbstractItemModel on top of it.

    Hope it helps

Log in to reply