Cpp代码
/**
* @brief 通用树形结构类
*/
class TreeItem
{
public:
TreeItem(const QList<QVariant>&data,TreeItem *parent=0 )
~TreeItem()
void appendChild(TreeItem *child)
TreeItem *child(int row)
int childCount() const
int columnCount() const
QVariant data(int column) const
int row() const
TreeItem *parent()
private:
TreeItem *parentItem // 父结点
QList<TreeItem*>childItems// 子结点列表
QList<QVariant>itemData// 子节点对应数据
}
二. 树形model实现
Cpp代码
#include <QAbstractItemModel>
#include "TreeItem.h"
class TagTreeModel : public QAbstractItemModel
{
Q_OBJECT
public:
TagTreeModel(QObject *parent = 0)
~TagTreeModel()
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
// 构建模型数据
void setupModelData(TreeItem *parent)
// 更新模型数据
void updateData()
private:
TreeItem *rootItem// 最顶层顶根节点(一个无效的QModelIndex)
}
TagTreeModel.cpp
Cpp代码
#include "TagTreeModel.h"
#include <QtGui>
TagTreeModel::TagTreeModel(QObject *parent):QAbstractItemModel(parent)
{
rootItem=NULL
updateData()
}
TagTreeModel::~TagTreeModel()
{
delete rootItem
}
void TagTreeModel::updateData()
{
// 废弃旧的模型数据
if(rootItem)
{
delete rootItem
rootItem=NULL
}
QList<QVariant>rootData
rootData<<"Tag"<<"Type"
rootItem=new TreeItem(rootData)
setupModelData(rootItem)
// 刷新模型
reset()
}
QVariant TagTreeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant()
// 添加图标
if(role==Qt::DecorationRole&&index.column()==0)
return QIcon("images/fold.png")
// 显示节点数据值
if(role==Qt::DisplayRole)
{
TreeItem *item=static_cast<TreeItem*>(index.internalPointer())
return item->data(index.column())
}
return QVariant()
}
Qt::ItemFlags TagTreeModel::flags(const QModelIndex &index) const
{
if(!index.isValid())
return 0
return Qt::ItemIsEnabled|Qt::ItemIsSelectable
}
QVariant TagTreeModel::headerData(int section, Qt::Orientation orientation,int role) const
{
if(orientation==Qt::Horizontal&&role==Qt::DisplayRole)
return rootItem->data(section)
return QVariant()
}
QModelIndex TagTreeModel::index(int row, int column,const QModelIndex &parent) const
{
if(!hasIndex(row,column,parent))
return QModelIndex()
TreeItem *parentItem
if(!parent.isValid())
{
parentItem=rootItem
}else
{
parentItem=static_cast<TreeItem*>(parent.internalPointer())
}
TreeItem *childItem=parentItem->child(row)
if(childItem)
return createIndex(row,column,childItem)// 展开树形,为子节点建立索引
else
return QModelIndex()
}
QModelIndex TagTreeModel::parent(const QModelIndex &index) const
{
if(!index.isValid())
return QModelIndex()
TreeItem *childItem=static_cast<TreeItem*>(index.internalPointer())
TreeItem *parentItem=childItem->parent()
// 顶层节点,直接返回空索引
if(parentItem==rootItem)
return QModelIndex()
// 为父结点建立索引
return createIndex(parentItem->row(),0,parentItem)
}
int TagTreeModel::rowCount(const QModelIndex &parent) const
{
TreeItem *parentItem
if(!parent.isValid())
parentItem=rootItem
else
parentItem=static_cast<TreeItem*>(parent.internalPointer())
return parentItem->childCount()// 返回父结点下子结点数目
}
int TagTreeModel::columnCount(const QModelIndex &parent ) const
{
return rootItem->columnCount()
}
// 设置模型数据,构建包含10个根结点,每个根结点包含两个子节点的树形结构
void TagTreeModel::setupModelData(TreeItem *parent)
{
for(int i=0i<10i++)
{
QList<QVariant>datas
datas<<QString("设备-%1").arg(i+1)<<QString("类型-%1").arg(i+1)
// 主结点下挂两个子节点
TreeItem *primary=new TreeItem(datas,parent)
parent->appendChild(primary)
for(int j=0j<2j++)
{
QList<QVariant>ds
ds<<QString("子设备-%1").arg(j+1)<<QString("子类型-%1").arg(j+1)
primary->appendChild(new TreeItem(ds,primary))
}
}
}
1、在TreeView的属性栏找到Nodes。2、点击,d出TreeNode编辑器窗口。
3、添加根按钮可以为TreeView控件添加根结点,添加子级按钮变为可用。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)