树形控件QTreeView使用自定义模型model
2013-03-24 12:21
381 查看
本项目代码已经上传至CSDN资源下载板块 http://download.csdn.net/detail/liuguangzhou123/5175389
模型主要代码如下:
//TreeModel.h
#ifndef
TREEMODEL_H
//TreeModel.cpp
#include
"TreeModel.h"
模型主要代码如下:
//TreeModel.h
#ifndef
TREEMODEL_H
#define TREEMODEL_H
#include <QAbstractItemModel>
#include <QStandardItemModel>
#include "DevRoot.h"
class TreeModel : public QAbstractItemModel
{
Q_OBJECT
public:
TreeModel(QObject *parent = 0);
~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:
void setupModelData(/*const QStringList &lines, TreeItem *parent*/);
CDevRoot *m_pDevRoot;
};
#endif // TREEMODEL_H
//TreeModel.cpp
#include
"TreeModel.h"
#include <QStringList>
#include <qDebug>
#include "NodeAA.h"
#include "NodeBB.h"
#include "NodeCC.h"
TreeModel::TreeModel(QObject *parent)
: QAbstractItemModel(parent)
{
m_pDevRoot = new CDevRoot("根目录");
setupModelData();
}
TreeModel::~TreeModel()
{
delete m_pDevRoot;
}
QVariant TreeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
{
return QVariant();
}
else if (role == Qt::DisplayRole)
{
CNodeBase *item = static_cast<CNodeBase*>(index.internalPointer());
return item->getName();
}
else if(role == Qt::DecorationRole)
{
CNodeBase *item = static_cast<CNodeBase*>(index.internalPointer());
if(item->getNodeType() == MAX_LEN_NODE_NAME)
return QIcon(":/images/window.bmp");
return QIcon(":/images/test.png");
}
else if(role == Qt::CheckStateRole)
{
}
else if(role == Qt::UserRole)
{
}
return QVariant();
}
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 m_pDevRoot->getName();
return QVariant();
}
QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
CNodeBase *pNodeBase;
if (!parent.isValid())//ROOT下挂装置NodeMac
{
CNodeBase *pNodeMac = m_pDevRoot->getNodeMac(row);
return createIndex(row, column, pNodeMac);
}
pNodeBase = static_cast<CNodeBase*>(parent.internalPointer());
if (pNodeBase->getNodeType() == NODE_TYPE_MAC && row == 0)//装置NodeMac下挂装置AA
{
CNodeMac *pNodeMac = static_cast<CNodeMac*>(parent.internalPointer());
CNodeAA *P = pNodeMac->getChildDeviceAA();
return createIndex(row,column,P);
}
else if (pNodeBase->getNodeType() == NODE_TYPE_MAC && row == 1)//装置NodeMac下挂装置BB
{
CNodeMac *pNodeMac = static_cast<CNodeMac*>(parent.internalPointer());
CNodeBB *pNodeDeviceGroupFolder = pNodeMac->getChildDeviceBB();
return createIndex(row,column,pNodeDeviceGroupFolder);
}
else if (pNodeBase->getNodeType() == NODE_TYPE_MAC && row == 2)//装置NodeMac下挂装置CC
{
CNodeMac *pNodeMac = static_cast<CNodeMac*>(parent.internalPointer());
CNodeCC *pNodeDeviceGroupFolder = pNodeMac->getChildDeviceCC();
return createIndex(row,column,pNodeDeviceGroupFolder);
}
else if (pNodeBase->getNodeType() == NODE_TYPE_AA)//装置AA下挂装置AA-CHILD
{
CNodeAA *pNodeDeviceFolder = static_cast<CNodeAA*>(parent.internalPointer());
CNodeAAChildA *pNodeBase = pNodeDeviceFolder->getChildDevice(row);
return createIndex(row,column,pNodeBase);
}
else if (pNodeBase->getNodeType() == NODE_TYPE_AA_CHILD)//装置AA-CHILD下挂装置
{
CNodeAAChildA *pNodeDeviceFolder = static_cast<CNodeAAChildA*>(parent.internalPointer());
CNodeBase *pNodeBase = pNodeDeviceFolder->getChildDevice(row);
return createIndex(row,column,pNodeBase);
}
else if (pNodeBase->getNodeType() == NODE_TYPE_BB)//装置BB下挂装置BB-CHILD
{
CNodeBB *pNodeDeviceGroupFolder = static_cast<CNodeBB*>(parent.internalPointer());
CNodeBase *pNodeBase = pNodeDeviceGroupFolder->getChildDeviceGroup(row);
return createIndex(row,column,pNodeBase);
}
else if (pNodeBase->getNodeType() == NODE_TYPE_CC)//装置CC下挂装置CC-CHILD
{
CNodeCC *pNodeDeviceGroupFolder = static_cast<CNodeCC*>(parent.internalPointer());
CNodeCCChild *pNodeBase = pNodeDeviceGroupFolder->getChildDevice(row);
return createIndex(row,column,pNodeBase);
}
else if (pNodeBase->getNodeType() == NODE_TYPE_CC_CHILD)//装置CC-CHILD下挂装置CC-CHILD-CHILD
{
CNodeCCChild *pNodeDeviceGroupFolder = static_cast<CNodeCCChild*>(parent.internalPointer());
CNodeCCChildChild *pNodeBase = pNodeDeviceGroupFolder->getChildDevice(row);
return createIndex(row,column,pNodeBase);
}
else if (pNodeBase->getNodeType() == NODE_TYPE_CC_CHILD_CHILD)//装置CC-CHILD-CHILD下挂装置
{
CNodeCCChildChild *pNodeDeviceGroupFolder = static_cast<CNodeCCChildChild*>(parent.internalPointer());
CNodeBase *pNodeBase = pNodeDeviceGroupFolder->getChildDevice(row);
return createIndex(row,column,pNodeBase);
}
return QModelIndex();
}
QModelIndex TreeModel::parent(const QModelIndex &index) const
{
if (!index.isValid())
{
return QModelIndex();
}
CNodeBase *childItem = static_cast<CNodeBase*>(index.internalPointer());
CNodeBase *parentItem = childItem->getParent();
if (parentItem->getNodeType() == NODE_TYPE_MAC_MANAGER)//设备管理
{
return QModelIndex();
}
else if(parentItem->getNodeType() == NODE_TYPE_MAC)//设备
{
return createIndex(0, 0, parentItem);
}
else if(parentItem->getNodeType() == NODE_TYPE_AA)//设备下挂装置AA
{
return createIndex(0, 0, parentItem);
}
else if(parentItem->getNodeType() == NODE_TYPE_AA_CHILD)//装置AA下挂装置
{
return createIndex(0, 0, parentItem);
}
else if(parentItem->getNodeType() == NODE_TYPE_BB)//设备下挂装置BB
{
return createIndex(1, 0, parentItem);
}
else if(parentItem->getNodeType() == NODE_TYPE_CC)//设备下挂装置CC
{
return createIndex(2, 0, parentItem);
}
else if(parentItem->getNodeType() == NODE_TYPE_CC_CHILD)//装置CC下挂装置CC-CHILD
{
return createIndex(0, 0, parentItem);
}
else if(parentItem->getNodeType() == NODE_TYPE_CC_CHILD_CHILD)//装置CC-CHILD-CHILD下挂装置
{
return createIndex(0, 0, parentItem);
}
return QModelIndex();
}
int TreeModel::rowCount(const QModelIndex &parent) const
{
CNodeBase *parentItem;
if (parent.column() > 0)
{
return 0;
}
if (!parent.isValid())
{
return m_pDevRoot->getMacCount();
}
parentItem = static_cast<CNodeBase*>(parent.internalPointer());
if(parentItem->getNodeType() == NODE_TYPE_MAC)//设备下挂数量
{
CNodeMac *p = static_cast<CNodeMac*>(parent.internalPointer());
return p->getChildCount();
}
else if(parentItem->getNodeType() == NODE_TYPE_AA)//装置AA下挂数量
{
CNodeAA *p = static_cast<CNodeAA*>(parent.internalPointer());
return p->getChildCount();
}
else if(parentItem->getNodeType() == NODE_TYPE_AA_CHILD)//装置AA下挂装置AA-CHILD下挂数量
{
CNodeAAChildA *p = static_cast<CNodeAAChildA*>(parent.internalPointer());
return p->getChildCount();
}
else if(parentItem->getNodeType() == NODE_TYPE_BB)//装置BB下挂数量
{
CNodeBB *p = static_cast<CNodeBB*>(parent.internalPointer());
return p->getChildCount();
}
else if(parentItem->getNodeType() == NODE_TYPE_CC)//装置CC下挂数量
{
CNodeCC *p = static_cast<CNodeCC*>(parent.internalPointer());
return p->getChildCount();
}
else if(parentItem->getNodeType() == NODE_TYPE_CC_CHILD)//装置CC-CHILD下挂数量
{
CNodeCCChild *p = static_cast<CNodeCCChild*>(parent.internalPointer());
return p->getChildCount();
}
else if(parentItem->getNodeType() == NODE_TYPE_CC_CHILD_CHILD)//装置CC-CHILD-CHILD下挂数量
{
CNodeCCChildChild *p = static_cast<CNodeCCChildChild*>(parent.internalPointer());
return p->getChildCount();
}
return 0;
}
int TreeModel::columnCount(const QModelIndex &parent) const
{
return 1;
}
void TreeModel::setupModelData()
{
CNodeMac *p = new CNodeMac(m_pDevRoot);
m_pDevRoot->appendChlid(p);
}
相关文章推荐
- Qt树形控件QTreeView使用1——节点的添加删除操作 复选框的设置
- Qt树形控件QTreeView使用1——节点的添加删除操作
- Qt树形控件QTreeView使用2——复选框的设置
- Qt树形控件QTreeView使用1——节点的添加删除操作
- Qt树形控件QTreeView使用1——节点的添加删除操作 复选框的设置
- Qt QTreeView 加 自定义Model实现自定义树形控件
- 在sklearn.model_selection.GridSearchCV中使用自定义验证集进行模型调参
- Qt树形控件QTreeView使用2——复选框的设置
- Qt树形控件QTreeView使用1——节点的添加删除操作
- Qt树形控件QTreeView使用
- Qt树形控件QTreeView使用1——节点的添加删除操作
- 使用预定义模型 QStringListModel例子
- ASP.NET 自定义服务器控件的使用
- 树形控件(Tree)的使用
- Qt编程25:QDirModel与QTreeView使用显示目录结构
- Android ListView控件的使用(自定义适配器 + ListView + 解决OOM )
- MFC中使用GDI+绘制自定义的控件
- [转].net自定义验证控件CustomValidator的使用
- 使用预定义模型 QStringListModel+QStringlist例子
- iOS无法使用自定义密码输入控件(自带键盘)