树形控件QTreeView使用自定义模型model

本项目代码已经上传至CSDN资源下载板块 http://download.csdn.net/detail/liuguangzhou123/5175389

 

 

 

模型主要代码如下:

//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);
}
 

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值