PDA

View Full Version : Help needed in creating Tree Model.



kaushal_gaurav
18th December 2008, 09:19
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

wysota
18th December 2008, 09:51
So, what have you already tried?

kaushal_gaurav
18th December 2008, 10:29
I have tried to populate the model in the models constructor



{
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().t oString();
QString typeStr = model.index(i,DevicesModel::TYPE).data().toString( );
QString bandStr = model.index(i,DevicesModel::BAND).data().toString( );

QStandardItem *manufacturerItem = new QStandardItem(manufacturetStr);
QStandardItem *typeItem = new QStandardItem(typelStr);
QStandardItem *bandItem = new QStandardItem(bandStr);

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

wysota
18th December 2008, 10:31
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.

kaushal_gaurav
18th December 2008, 10:43
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
-----------|

wysota
18th December 2008, 12:14
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.