QTreeView with Model-View help
Hi,
I am trying to make a tree for my data objects, but I am having trouble figuring out how I should approach it to make it as clean as possible. I was using a QTreeWidget, but it felt very hackish and wasn't very nice to keep up with. I'd like to use a QTreeView, but I am unsure of how to handle the models. An example of how my data classes work is below.
- data class 1
- data class 2
At the root, there are the large data classes, then there are groups of related data classes within that (the groups keep unrelated data classes from grouping together). When a data class is added, it needs to build its subtree of data and groups. the group "labels" cant be selected, but the data classes each have a widget for editing the internal data. How should I design the model to handle this?
--MathStuf
Edit: Typoed QList* rather than QTree*
Re: QTreeView with Model-View help
Shouldn't you be using QTreeView and not QListView?
Re: QTreeView with Model-View help
Wow, that's a typo. I'll edit to fix that.
--MathStuf
Re: QTreeView with Model-View help
Quote:
Originally Posted by
MathStuf
How should I design the model to handle this?
Could you explain what do you mean by the word "how" here? :) The question is very general, so it's hard to provide a detailed answer to your question.
Re: QTreeView with Model-View help
I would like to know how I could got the model to handle the different types of data classes without being a huge if else tree. If that's the only way, I'll make it work, but I was wondering if something like template or subclassing could work here. I'm not sure how the internals of the model-view work and if that would work at all.
--MathStuf
Re: QTreeView with Model-View help
I'd go for subclassing. Make your elements derived from a common base class with virtual methods and delegate the model's methods to items using virtual methods.
Re: QTreeView with Model-View help
All of my data classes are derived from a common base class. Not all of the data classes have the same getter/setter methods, so there are different widgets for each, again which are derived from a common base class.
If you mean subclassing the models for each data class , how would i get the QTreeView to use a different model depending on the item type? If I'm misinterpreting what you're saying, let me know.
I think one of the places I keep tripping over is how to figure out is how to keep the grouping entries from getting confused with the data entries. I wish that this would just straighten itself out in my mind like the rest of the "how should I do this?" problems have with this project.
--Ben
Re: QTreeView with Model-View help
I mean exactly what I wrote :) Provide a common interface for all types of objects your tree carries and use the interface from within the model. For instance:
Code:
class Base {
public:
QVariant data
(int role
=Qt
::DisplayRole) const = 0;
};
class Level1Item : public Base {
public:
QVariant data
(int role
=Qt
::DisplayRole) const { return myName;
}
};
Base *ptr = static_cast<Base*>(index.internalPointer());
return ptr->data(role);
}
Re: QTreeView with Model-View help
Oh, ok. I see what you mean now. Thanks a lot.
--MathStuf
Re: QTreeView with Model-View help
I have another question with this. Each model can change the number of children rows from within (i.e. the Level1Item class above) with setData. Do I have to signal beginRemoveRows/beginInsertRows and their end equivalent?
--MathStuf
Re: QTreeView with Model-View help
Yes, you have to emit some signals. Emitting layoutChanged() or even reset() might be an option instead of using begin/end*Rows.