// file: enumitemmodel.h
#ifndef ENUMITEMMODEL_H
#define ENUMITEMMODEL_H
#include <QAbstractItemModel>
#include <QModelIndex>
#include <QVariant>
#include <QPair>
#include <QList>
#include <QStringList>
typedef QPair<QString,QStringList> EnumDefinition;
typedef QList<EnumDefinition> EnumDefinitionList;
{
Q_OBJECT
public:
EnumItemModel
(QObject *parent
= 0);
virtual ~EnumItemModel();
EnumDefinitionList enumTypes;
private:
void setupModelData();
};
#endif
// file: enumitemmodel.cpp
#include <QtGui>
#include <iostream>
#include "enumitemmodel.h"
EnumItemModel
::EnumItemModel(QObject *parent
){
setupModelData();
}
EnumItemModel::~EnumItemModel()
{
}
int EnumItemModel
::columnCount(const QModelIndex &parent
) const {
return 1;
}
{
if (!index.isValid())
if (role != Qt::DisplayRole)
if (!parentIndex.isValid())
return QVariant(enumTypes
[index.
row()].
first);
int parentRow = parentIndex.row();
return QVariant(enumTypes
[parentRow
].
second[index.
row()]);
}
Qt
::ItemFlags EnumItemModel
::flags(const QModelIndex &index
) const{
if (!index.isValid())
return 0;
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
const
{
if (!hasIndex(row, column, parent))
return createIndex(row, column, NULL);
}
{
if (!index.isValid())
return index.parent();
}
int EnumItemModel
::rowCount(const QModelIndex &parent
) const {
if (parent.column() > 0)
return 0;
if (!parent.isValid())
return 0;
return enumTypes[parent.row()].second.count();
}
void EnumItemModel::setupModelData()
{
booleanValues.append("False");
booleanValues.append("True");
enumTypes.append(EnumDefinition("Boolean", booleanValues));
colorValues.append("Red");
colorValues.append("Green");
colorValues.append("Blue");
enumTypes.append(EnumDefinition("Color", colorValues));
}
// file: main.cpp
#include <QtGui>
#include "enumitemmodel.h"
int main(int argc, char *argv[])
{
EnumItemModel model;
view.setModel(&model);
view.
setWindowTitle(QObject::tr("Simple Tree Model"));
view.show();
return app.exec();
}
// file: enumitemmodel.h
#ifndef ENUMITEMMODEL_H
#define ENUMITEMMODEL_H
#include <QAbstractItemModel>
#include <QModelIndex>
#include <QVariant>
#include <QPair>
#include <QList>
#include <QStringList>
typedef QPair<QString,QStringList> EnumDefinition;
typedef QList<EnumDefinition> EnumDefinitionList;
class EnumItemModel : public QAbstractItemModel
{
Q_OBJECT
public:
EnumItemModel(QObject *parent = 0);
virtual ~EnumItemModel();
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;
EnumDefinitionList enumTypes;
private:
void setupModelData();
};
#endif
// file: enumitemmodel.cpp
#include <QtGui>
#include <iostream>
#include "enumitemmodel.h"
EnumItemModel::EnumItemModel(QObject *parent)
: QAbstractItemModel(parent)
{
setupModelData();
}
EnumItemModel::~EnumItemModel()
{
}
int EnumItemModel::columnCount(const QModelIndex &parent) const
{
return 1;
}
QVariant EnumItemModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role != Qt::DisplayRole)
return QVariant();
const QModelIndex &parentIndex = index.parent();
if (!parentIndex.isValid())
return QVariant(enumTypes[index.row()].first);
int parentRow = parentIndex.row();
return QVariant(enumTypes[parentRow].second[index.row()]);
}
Qt::ItemFlags EnumItemModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return 0;
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
QModelIndex EnumItemModel::index(int row, int column, const QModelIndex &parent)
const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
return createIndex(row, column, NULL);
}
QModelIndex EnumItemModel::parent(const QModelIndex &index) const
{
if (!index.isValid())
return QModelIndex();
return index.parent();
}
int EnumItemModel::rowCount(const QModelIndex &parent) const
{
if (parent.column() > 0)
return 0;
if (!parent.isValid())
return 0;
return enumTypes[parent.row()].second.count();
}
void EnumItemModel::setupModelData()
{
QStringList booleanValues;
booleanValues.append("False");
booleanValues.append("True");
enumTypes.append(EnumDefinition("Boolean", booleanValues));
QStringList colorValues;
colorValues.append("Red");
colorValues.append("Green");
colorValues.append("Blue");
enumTypes.append(EnumDefinition("Color", colorValues));
}
// file: main.cpp
#include <QtGui>
#include "enumitemmodel.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
EnumItemModel model;
QTreeView view;
view.setModel(&model);
view.setWindowTitle(QObject::tr("Simple Tree Model"));
view.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks