[Interest] Model for QTreeView
Alex Strickland
sscc at mweb.co.za
Mon Nov 19 19:25:25 CET 2012
On 2012/11/19 06:27 PM, Alex Strickland wrote:
> Yes, I have, but as you might judge from the above, it's still a bit hazy!
My attempts don't work, can anyone say why?
TIA
--
Regards
Alex
-------------- next part --------------
/*
* Copyright Electric Bridge Software 2011,2012.
*/
#include "treemodel.h"
TreeModel::TreeModel()
{
TreeStruct treeStruct;
treeStruct.name = "one";
treeStruct.leaves << "one/one" << "one/two";
m_treeData << treeStruct;
treeStruct.name = "two";
treeStruct.leaves.clear();
m_treeData << treeStruct;
treeStruct.name = "three";
treeStruct.leaves << "three/one" << "three/two" << "three/three";
m_treeData << treeStruct;
}
QVariant TreeModel::data(const QModelIndex &index,
int role) const
{
if (!index.isValid())
return QVariant();
if (role != Qt::DisplayRole)
return QVariant();
if (index.internalId() >= 0)
return QVariant(m_treeData[ index.internalId() ].leaves[ index.row() ]);
return QVariant(m_treeData[ index.row() ].name);
}
Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return 0;
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
QVariant TreeModel::headerData(int section,
Qt::Orientation orientation,
int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
return QVariant("header");
return QVariant();
}
QModelIndex TreeModel::index(int row,
int column,
const QModelIndex &parent) const
{
QModelIndex modelIndex;
if (!hasIndex(row, column, parent)) {
modelIndex = QModelIndex();
}
else {
// Then it's the root, and we can return an id of row via createIndex
if (!parent.isValid())
modelIndex = createIndex(row, column, row);
else
// else it's not the root, and the id is -1
modelIndex = createIndex(row, column, -1);
}
return modelIndex;
}
QModelIndex TreeModel::parent(const QModelIndex &index) const
{
QModelIndex modelIndex;
if (!index.isValid()) {
modelIndex = QModelIndex();
}
else {
// it's a leaf, so the parent is
if (index.internalId() == -1) {
modelIndex = createIndex(index.row(),
index.column(),
index.row());
}
else {
// else it's the parent and we must return an invalid Model Index
modelIndex = QModelIndex();
}
}
return modelIndex;
}
int TreeModel::rowCount(const QModelIndex &parent) const
{
int rowCount;
if (!parent.isValid())
rowCount = m_treeData.length();
else
rowCount = m_treeData[ parent.row() ].leaves.length();
return rowCount;
}
int TreeModel::columnCount(const QModelIndex &parent) const
{
return 1;
}
-------------- next part --------------
/*
* Copyright Electric Bridge Software 2011,2012.
*/
#ifndef TREEMODEL_H
#define TREEMODEL_H
#include <QAbstractItemModel>
#include <QModelIndex>
#include <QVariant>
typedef struct TreeStruct
{
QString name;
QList<QString> leaves;
};
class TreeModel : public QAbstractItemModel
{
public:
TreeModel();
QVariant data(const QModelIndex &index,
int role) const;
Qt::ItemFlags flags(const QModelIndex &index) const;
QVariant headerData(int section,
Qt::Orientation orientation,
int role = Qt::DisplayRole) 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:
QList<TreeStruct> m_treeData;
};
#endif // TREEMODEL_H
More information about the Interest
mailing list