PDA

View Full Version : QTreeView with Model-View help



MathStuf
21st April 2008, 20:01
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

group

sub-data class 1

group

sub-sub-data class 1



group
group

data class 2

group
group
group



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*

wysota
21st April 2008, 20:49
Shouldn't you be using QTreeView and not QListView?

MathStuf
21st April 2008, 20:58
Wow, that's a typo. I'll edit to fix that.

--MathStuf

wysota
21st April 2008, 21:24
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.

MathStuf
21st April 2008, 21:41
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

wysota
21st April 2008, 23:41
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.

MathStuf
22nd April 2008, 00:27
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

wysota
22nd April 2008, 00:52
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:

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;
}
};

QVariant Model::data(const QModelIndex &index, int role){
if(!index.isValid()) return QVariant();
Base *ptr = static_cast<Base*>(index.internalPointer());
return ptr->data(role);
}

MathStuf
22nd April 2008, 01:47
Oh, ok. I see what you mean now. Thanks a lot.

--MathStuf

MathStuf
25th April 2008, 22:18
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

wysota
26th April 2008, 07:23
Yes, you have to emit some signals. Emitting layoutChanged() or even reset() might be an option instead of using begin/end*Rows.