September 16, 2011

Kiko007 Kiko007
Lab Rat
5 posts

How can create QTreeView with different Objects


This is my doubt, I have 4 classes, Institution, College, Careers and Department. Each one of them has a name and other attributes, but what interests me is the name only. I want to show in a QTreeView and therefore make a subclassing of QAbstractItemModel, this is where my doubt, trying to reimplement the index, parent and rowCount functions. I do it with class Institution, but with the other hierarchies I lose, I really do not know how to refer to them.

This is the code of the classes, how to reimplement the index, parent and rowCount functions?

  1. Class Institution {
  3. public:
  4. Institution(const QString aName);
  6. QSting name;
  7. QList<College*> child;
  8. };
  11. Class College {
  13. public:
  14. // Here I initialize the name and parent
  15. College(const QString aName, Institution *aParent = 0);
  17. QSting name;
  18. QList<Careers*> child;
  19. Institution *parent;
  20. };
  22. Class Careers {
  24. public:
  25. // Here I initialize the name and parent
  26. Careers(const QString aName, College *aParent = 0);
  28. QSting name;
  29. QList<Department*> child;
  30. College *parent;
  31. };
  33. Class Department {
  35. public:
  36. // Here I initialize the name and parent
  37. Department(const QString aName, Careers *aParent = 0);
  39. QSting name;
  40. Careers *parent;
  41. };

Help me, please!
Thanks you

2 replies

September 16, 2011

Volker Volker
Ant Farmer
5331 posts

The information you gave is far too vague, we don’t know what you want to do.

There are some tutorials and examples in the docs and the wiki:

I’d advice you to start with one of them.

Four your design of the different objects:
You could create a common base class that defines the name() method. In your model storage class use only this base class.

September 17, 2011

Andre Andre
Robot Herder
6662 posts

Another approach is to create a struct like this:

  1. struct TreeNode {
  2.   enum TreeNodeType {
  3.     Institution,
  4.     College,
  5.     Career,
  6.     Department
  7.   };
  9.   TreeNodeType type;
  10.   void* object;
  11.   QList<TreeNode> childNodes;
  12. }

Instead of a void pointer and a type flag (I know, not very type save), you can of course also use other solutions like having a pointer to each of the four different classes in there (and have only one be non-0) or even to have a base TreeNode class, and make four subclasses from it, one for each of the types you need to store.

Your index pointer in your QModelIndex will point to the TreeNode class, not to your instances directly. That way, you can be sure what to case your pointer to. Again, an alternative for that is to use a single (flat) list of nodes in the background, and put the index in this list as the item data in your QModelIndex.

However you do it: tree models stay hard to get right, and the way QAIM and QModelIndex are constructed does not make it easier to create mixed-type trees.

  ‹‹ string replace content      Custom memory management ››

You must log in to post a reply. Not a member yet? Register here!