PDA

View Full Version : Writing a Tree model.



kaushal_gaurav
15th January 2009, 09:18
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

kaushal_gaurav
16th January 2009, 05:09
any body....
any idea...

spirit
16th January 2009, 06:18
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/

kaushal_gaurav
16th January 2009, 07:06
I have written the followinf code to create a tree.


for (int i = 0; i < m_manufacturerList.count(); ++i)
{
QStandardItem *item = new QStandardItem(m_manufacturerList.at(i));
parentItem = item;
QList<QString> modelList = m_pPrivate->m_ManufacturerModel.values(item->text());
parentItem->appendRow(modelList);

for(j = 0; j < modelList.count(); ++j)
{
parentItem = modelList.at(i);
parentItem->appendRow(m_pPrivate->m_ModelBand.values(modelList.at(i)));
}
}

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

kaushal_gaurav
16th January 2009, 11:05
Hi,

I have done this so far...

TreeModel.h




#ifndef ADD_DEVICES_TREE_PROXY_MODEL_H
#define ADD_DEVICES_TREE_PROXY_MODEL_H

#include <QAbstractItemModel>
#include <QModelIndex>
#include <QVariant>

class AddDeviceTreeProxyModel : public QAbstractItemModel
{
Q_OBJECT

public:
AddDeviceTreeProxyModel(QObject *parent = 0);
~AddDeviceTreeProxyModel();

QVariant data(const QModelIndex &index, int role) const;
Qt::ItemFlags flags(const QModelIndex &index) const;
QModelIndex index(int row, int column,
const QModelIndex &parent = QModelIndex()) const;
QModelIndex parent(const QModelIndex &index) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;

private:
class Private;
Private* const m_pPrivate;
};

#endif // ADD_DEVICES_TREE_PROXY_MODEL_H



TreeModel.cpp



#include "TreeModel.h"
#include <QStandardItem>

class AddDeviceTreeProxyModel::Private
{
public:
// Constructor.
Private( AddDeviceTreeProxyModel* deviceTree )
: m_AddDeviceTreeProxyModel(deviceTree)
{
}

void setupModelData();
QStringList m_manufacturerList; // Manufacturer List.
QMultiMap <QString, QString> m_ManufacturerModel; // Multimap of Manufacturer and Model.
QMultiMap <QString, QString> m_ModelBand; // Multimap of Model and band.

private:
AddDeviceTreeProxyModel* const m_AddDeviceTreeProxyModel;
};

AddDeviceTreeProxyModel::AddDeviceTreeProxyModel(Q Object *parent)
: QAbstractItemModel(parent),
m_pPrivate( new Private( this ))
{
for (int i = 0; i < 5 ; ++i)
{
m_pPrivate->m_manufacturerList.append(tr("Manf%1").arg(i));
}

for (int i = 0; i < m_pPrivate->m_manufacturerList.count() ; ++i)
{
m_pPrivate->m_ManufacturerModel.insert(tr("Manf%1").arg(i), "Model1");
m_pPrivate->m_ManufacturerModel.insert(tr("Manf%1").arg(i), "Model2");
}

for (int i = 0; i < m_pPrivate->m_manufacturerList.count() ; ++i)
{
m_pPrivate->m_ModelBand.insert(tr("Model1").arg(i), "Band1");
m_pPrivate->m_ModelBand.insert(tr("Model1").arg(i), "Band2");
m_pPrivate->m_ModelBand.insert(tr("Model2").arg(i), "Band1");
m_pPrivate->m_ModelBand.insert(tr("Model2").arg(i), "Band2");
}

m_pPrivate->setupModelData();
}

AddDeviceTreeProxyModel::~AddDeviceTreeProxyModel( )
{
delete m_pPrivate;
}

int AddDeviceTreeProxyModel::columnCount(const QModelIndex &parent) const
{
return 0;
}

QVariant AddDeviceTreeProxyModel::data(const QModelIndex &index, int role) const
{
return QVariant();
}

Qt::ItemFlags AddDeviceTreeProxyModel::flags(const QModelIndex &index) const
{
return Qt::ItemIsEditable;

}

QModelIndex AddDeviceTreeProxyModel::index(int row, int column, const QModelIndex &parent) const
{
return QModelIndex();
}

QModelIndex AddDeviceTreeProxyModel::parent(const QModelIndex &index) const
{
return QModelIndex();
}

int AddDeviceTreeProxyModel::rowCount(const QModelIndex &parent) const
{
return 0;
}

void AddDeviceTreeProxyModel::Private::setupModelData()
{
}



Main.cpp


#include <QApplication>
#include "TreeModel.h"
#include "ui_Form.h"

int main(int argc, char** argv)
{
QApplication a(argc, argv);

QWidget form;
Ui::Form ui;
ui.setupUi(&form);
form.show();

AddDeviceTreeProxyModel model;
ui.listView->setModel( &model );
ui.treeView->setModel( &model );

return a.exec();
}

Please tell me how do i implement rowCount(), columnCount(), index(), parent() functions....

I am stuck... badly

Regards,
GK

aamer4yu
16th January 2009, 11:15
Did you have a look at examples in Qt Demo ??

kaushal_gaurav
16th January 2009, 11:22
yes i have seen ... but could not mould it to implement mine funcitons....