Results 1 to 7 of 7

Thread: Writing a Tree model.

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

    Default Writing a Tree model.

    Hi,

    Can anyone help me in writing a Tree model from QStandardItemModel.
    I have all the required data in a seperate QStandardItemModel.
    i need to create a new Tree model or may be a proxy tree model.
    How do i implement a rowCount function for the TreeModel.
    I have already gone through SimpleTreeModel example but cound not complete my tree model.

    Please do help me with some code snippets...

    Regards,
    GK

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

    Default Re: Writing a Tree model.

    any body....
    any idea...

  3. #3
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Writing a Tree model.

    there is an example in Qt Assistant at QStandardItemModel
    An example usage of QStandardItemModel to create a tree:

    QStandardItemModel model;
    QStandardItem *parentItem = model.invisibleRootItem();
    for (int i = 0; i < 4; ++i) {
    QStandardItem *item = new QStandardItem(QString("item %0").arg(i));
    parentItem->appendRow(item);
    parentItem = item;
    }
    also lool at Qt's example QTDIR/examples/itemviews/simpletreemodel/
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

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

    Default Re: Writing a Tree model.

    I have written the followinf code to create a tree.
    Qt Code:
    1. for (int i = 0; i < m_manufacturerList.count(); ++i)
    2. {
    3. QStandardItem *item = new QStandardItem(m_manufacturerList.at(i));
    4. parentItem = item;
    5. QList<QString> modelList = m_pPrivate->m_ManufacturerModel.values(item->text());
    6. parentItem->appendRow(modelList);
    7.  
    8. for(j = 0; j < modelList.count(); ++j)
    9. {
    10. parentItem = modelList.at(i);
    11. parentItem->appendRow(m_pPrivate->m_ModelBand.values(modelList.at(i)));
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    I have a list m_manufacturerList which contins some items.
    then in have two multi maps..m_ManufacturerModel and m_ModelBand

    I need to show data in the list views... see the attachment.

    this is something like MAC finder...view...

    how do i implement fucntions... rowCount, index(), parent() etc..

    Regards,
    GK
    Attached Images Attached Images

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

    Default Re: Writing a Tree model.

    Hi,

    I have done this so far...

    TreeModel.h

    Qt Code:
    1. #ifndef ADD_DEVICES_TREE_PROXY_MODEL_H
    2. #define ADD_DEVICES_TREE_PROXY_MODEL_H
    3.  
    4. #include <QAbstractItemModel>
    5. #include <QModelIndex>
    6. #include <QVariant>
    7.  
    8. class AddDeviceTreeProxyModel : public QAbstractItemModel
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. AddDeviceTreeProxyModel(QObject *parent = 0);
    14. ~AddDeviceTreeProxyModel();
    15.  
    16. QVariant data(const QModelIndex &index, int role) const;
    17. Qt::ItemFlags flags(const QModelIndex &index) const;
    18. QModelIndex index(int row, int column,
    19. const QModelIndex &parent = QModelIndex()) const;
    20. QModelIndex parent(const QModelIndex &index) const;
    21. int rowCount(const QModelIndex &parent = QModelIndex()) const;
    22. int columnCount(const QModelIndex &parent = QModelIndex()) const;
    23.  
    24. private:
    25. class Private;
    26. Private* const m_pPrivate;
    27. };
    28.  
    29. #endif // ADD_DEVICES_TREE_PROXY_MODEL_H
    To copy to clipboard, switch view to plain text mode 

    TreeModel.cpp

    Qt Code:
    1. #include "TreeModel.h"
    2. #include <QStandardItem>
    3.  
    4. class AddDeviceTreeProxyModel::Private
    5. {
    6. public:
    7. // Constructor.
    8. Private( AddDeviceTreeProxyModel* deviceTree )
    9. : m_AddDeviceTreeProxyModel(deviceTree)
    10. {
    11. }
    12.  
    13. void setupModelData();
    14. QStringList m_manufacturerList; // Manufacturer List.
    15. QMultiMap <QString, QString> m_ManufacturerModel; // Multimap of Manufacturer and Model.
    16. QMultiMap <QString, QString> m_ModelBand; // Multimap of Model and band.
    17.  
    18. private:
    19. AddDeviceTreeProxyModel* const m_AddDeviceTreeProxyModel;
    20. };
    21.  
    22. AddDeviceTreeProxyModel::AddDeviceTreeProxyModel(QObject *parent)
    23. : QAbstractItemModel(parent),
    24. m_pPrivate( new Private( this ))
    25. {
    26. for (int i = 0; i < 5 ; ++i)
    27. {
    28. m_pPrivate->m_manufacturerList.append(tr("Manf%1").arg(i));
    29. }
    30.  
    31. for (int i = 0; i < m_pPrivate->m_manufacturerList.count() ; ++i)
    32. {
    33. m_pPrivate->m_ManufacturerModel.insert(tr("Manf%1").arg(i), "Model1");
    34. m_pPrivate->m_ManufacturerModel.insert(tr("Manf%1").arg(i), "Model2");
    35. }
    36.  
    37. for (int i = 0; i < m_pPrivate->m_manufacturerList.count() ; ++i)
    38. {
    39. m_pPrivate->m_ModelBand.insert(tr("Model1").arg(i), "Band1");
    40. m_pPrivate->m_ModelBand.insert(tr("Model1").arg(i), "Band2");
    41. m_pPrivate->m_ModelBand.insert(tr("Model2").arg(i), "Band1");
    42. m_pPrivate->m_ModelBand.insert(tr("Model2").arg(i), "Band2");
    43. }
    44.  
    45. m_pPrivate->setupModelData();
    46. }
    47.  
    48. AddDeviceTreeProxyModel::~AddDeviceTreeProxyModel()
    49. {
    50. delete m_pPrivate;
    51. }
    52.  
    53. int AddDeviceTreeProxyModel::columnCount(const QModelIndex &parent) const
    54. {
    55. return 0;
    56. }
    57.  
    58. QVariant AddDeviceTreeProxyModel::data(const QModelIndex &index, int role) const
    59. {
    60. return QVariant();
    61. }
    62.  
    63. Qt::ItemFlags AddDeviceTreeProxyModel::flags(const QModelIndex &index) const
    64. {
    65. return Qt::ItemIsEditable;
    66.  
    67. }
    68.  
    69. QModelIndex AddDeviceTreeProxyModel::index(int row, int column, const QModelIndex &parent) const
    70. {
    71. return QModelIndex();
    72. }
    73.  
    74. QModelIndex AddDeviceTreeProxyModel::parent(const QModelIndex &index) const
    75. {
    76. return QModelIndex();
    77. }
    78.  
    79. int AddDeviceTreeProxyModel::rowCount(const QModelIndex &parent) const
    80. {
    81. return 0;
    82. }
    83.  
    84. void AddDeviceTreeProxyModel::Private::setupModelData()
    85. {
    86. }
    To copy to clipboard, switch view to plain text mode 

    Main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include "TreeModel.h"
    3. #include "ui_Form.h"
    4.  
    5. int main(int argc, char** argv)
    6. {
    7. QApplication a(argc, argv);
    8.  
    9. QWidget form;
    10. Ui::Form ui;
    11. ui.setupUi(&form);
    12. form.show();
    13.  
    14. AddDeviceTreeProxyModel model;
    15. ui.listView->setModel( &model );
    16. ui.treeView->setModel( &model );
    17.  
    18. return a.exec();
    19. }
    To copy to clipboard, switch view to plain text mode 
    Please tell me how do i implement rowCount(), columnCount(), index(), parent() functions....

    I am stuck... badly

    Regards,
    GK

  6. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Writing a Tree model.

    Did you have a look at examples in Qt Demo ??

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

    Default Re: Writing a Tree model.

    yes i have seen ... but could not mould it to implement mine funcitons....

Similar Threads

  1. A few queries about Model View Programming
    By montylee in forum Qt Programming
    Replies: 46
    Last Post: 2nd March 2009, 08:36
  2. Creating a Tree Model from a QStandardModel
    By kaushal_gaurav in forum Qt Programming
    Replies: 3
    Last Post: 19th December 2008, 06:47
  3. Help needed in creating Tree Model.
    By kaushal_gaurav in forum Qt Programming
    Replies: 5
    Last Post: 18th December 2008, 12:14
  4. How use Model view with a tree wich have Cycle ?
    By weepdoo in forum Qt Programming
    Replies: 2
    Last Post: 9th December 2008, 17:05
  5. using the example of simple tree model
    By krishna.bv in forum Qt Programming
    Replies: 1
    Last Post: 22nd December 2006, 12:28

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.