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?
@Class Institution {
public:
Institution(const QString aName);QSting name;
QList<College*> child;
};Class College {
public:
// Here I initialize the name and parent
College(const QString aName, Institution *aParent = 0);QSting name;
QList<Careers*> child;
Institution *parent;
};Class Careers {
public:
// Here I initialize the name and parent
Careers(const QString aName, College *aParent = 0);QSting name;
QList<Department*> child;
College *parent;
};Class Department {
public:
// Here I initialize the name and parent
Department(const QString aName, Careers *aParent = 0);QSting name;
Careers *parent;
};
@Help me, please!
Thanks you -
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:
- "Item Views Examples ":http://developer.qt.nokia.com/doc/qt-4.7/examples-itemviews.html from the docs
- "Guide to Model/View programming":http://developer.qt.nokia.com/doc/qt-4.7/model-view-programming.html of the docs
- various forum threads
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. -
Another approach is to create a struct like this:
@
struct TreeNode {
enum TreeNodeType {
Institution,
College,
Career,
Department
};TreeNodeType type;
void* object;
QList<TreeNode> childNodes;
}
@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.
-
This post is deleted!