Results 1 to 6 of 6

Thread: Help needed in creating Tree Model.

  1. #1
    Join Date
    Jun 2008
    Posts
    89
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default 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
    Last edited by kaushal_gaurav; 18th December 2008 at 09:23.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Help needed in creating Tree Model.

    So, what have you already tried?

  3. #3
    Join Date
    Jun 2008
    Posts
    89
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Help needed in creating Tree Model.

    I have tried to populate the model in the models constructor

    Qt Code:
    1. {
    2. MyModel model;
    3.  
    4. m_Private->rootItem = model.invisibleRootItem();
    5.  
    6. for(int i =0; i < model.rowCount(); ++i)
    7. {
    8. for(int j = 0; j < model.columnCount(); ++j)
    9. {
    10. if(MyModel::MANUFACTURER == model.index(i, j).column())
    11. {
    12. QString manufacturetStr = model.index(i,DevicesModel::MANUFACTURER).data().toString();
    13. QString typeStr = model.index(i,DevicesModel::TYPE).data().toString();
    14. QString bandStr = model.index(i,DevicesModel::BAND).data().toString();
    15.  
    16. QStandardItem *manufacturerItem = new QStandardItem(manufacturetStr);
    17. QStandardItem *typeItem = new QStandardItem(typelStr);
    18. QStandardItem *bandItem = new QStandardItem(bandStr);
    19.  
    20. m_rootItem->appendRow(typeItem );
    21. m_rootItem = typeItem ;
    22. m_rootItem->appendRow(bandItem);
    23. m_rootItem = bandItem;
    24. }
    25. this->setData(index(i, j), manufacturerItem);
    26. }
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 

    I am having problem with inserting data into the model and how do i implement rowCount() and columnCount() functions

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.

  5. #5
    Join Date
    Jun 2008
    Posts
    89
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default 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
    -----------|

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.

Similar Threads

  1. Creating a Tree Model from a QStandardModel
    By kaushal_gaurav in forum Qt Programming
    Replies: 3
    Last Post: 19th December 2008, 06:47
  2. problem creating dom tree
    By dreamer in forum Qt Programming
    Replies: 2
    Last Post: 8th May 2008, 08:12
  3. creating a Tree
    By mickey in forum General Programming
    Replies: 0
    Last Post: 20th April 2008, 17:57
  4. using the example of simple tree model
    By krishna.bv in forum Qt Programming
    Replies: 1
    Last Post: 22nd December 2006, 12:28
  5. creating a tree hierarchy using QStandardModel
    By wind in forum Qt Programming
    Replies: 3
    Last Post: 19th September 2006, 21:54

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.