Help needed in creating Tree Model.
Hi,
Please help me in creating a tree model.
I need to create a model with parent - child relationship.
I have all the data present in another QStandardItemModel.
I think creating a new proxy model will do the job. But please help me in
how can i arrange data in the new model and what will be the row and column count of this
new model.
I need the data in the model to be structured in
item1 ----|
-----------|----SubItem1----|---SubSubItem1
-----------|-------------------|---SubSubItem2
-----------|
-----------|----SubItem2
-----------|----SubItem3
-----------|
item2 ----|
-----------|----SubItem1
-----------|----SubItem2 ----|---SubSubItem1
-----------|--------------------|---SubSubItem2
-----------|----SubItem3
-----------|
The hirearchy is just three level deep.
Please Help..........................
Regards,
GK
Re: Help needed in creating Tree Model.
So, what have you already tried?
Re: Help needed in creating Tree Model.
I have tried to populate the model in the models constructor
Code:
{
MyModel model;
m_Private->rootItem = model.invisibleRootItem();
for(int i =0; i < model.rowCount(); ++i)
{
for(int j = 0; j < model.columnCount(); ++j)
{
if(MyModel::MANUFACTURER == model.index(i, j).column())
{
QString manufacturetStr
= model.
index(i,DevicesModel
::MANUFACTURER).
data().
toString();
QString typeStr
= model.
index(i,DevicesModel
::TYPE).
data().
toString();
QString bandStr
= model.
index(i,DevicesModel
::BAND).
data().
toString();
m_rootItem->appendRow(typeItem );
m_rootItem = typeItem ;
m_rootItem->appendRow(bandItem);
m_rootItem = bandItem;
}
this->setData(index(i, j), manufacturerItem);
}
}
}
I am having problem with inserting data into the model and how do i implement rowCount() and columnCount() functions
Re: Help needed in creating Tree Model.
This way you will have a copy of the data in the original model. Is that your intention? Do you want to modify the data? If so, then implementing a proxy model would be better than making a new model with the copy of the data from the first one.
Re: Help needed in creating Tree Model.
No i do not want to copy data nor to modify data.
I just need it for display in list view...
The new model should have three columns but the orignal model has 10 columns but i just need data from the three columns and that too in a tree from.
item1 ----|
-----------|----SubItem1----|---SubSubItem1
-----------|-------------------|---SubSubItem2
-----------|
-----------|----SubItem2
-----------|----SubItem3
-----------|
item2 ----|
-----------|----SubItem1
-----------|----SubItem2 ----|---SubSubItem1
-----------|--------------------|---SubSubItem2
-----------|----SubItem3
-----------|
Re: Help needed in creating Tree Model.
You can either implement a proxy model that will reorder items and modify the number of columns and rows or create a new (tree) model with three columns and fill it with data yourself.
Really, showing the same ascii art all the time won't help. Giving us something compilable what we could run and see ourselves would be a much better idea.