1绑定数据到treeview(设计到配置文件)
2手动为treeview导入数据(写循环,添加数据也可以用写好的类库)
还有看到你写的要求貌似是求代码文件?
如果这样的话~可以给你几个例子~貌似这样的视频教程也有
我也知道在哪下
一. 树形结构体定义 treeitem.hCpp代码
/**
* @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))
}
}
}
BIAS0:= (C-MA(C,2))/MA(C,2)*100BIAS1 := (C-MA(C,12))/MA(C,12)*100
BIAS2 := (C-MA(C,26))/MA(C,26)*100
BIAS3 := (C-MA(C,48))/MA(C,48)*100
HXL:=V/CAPITAL*100
D1:=INDEXC
D2:=MA(D1,56)
DR2:=D1/D2<0.94
E1:=(C-HHV(C,12))/HHV(C,12)*10
E2:=(C-REF(C,26))/REF(C,26)*10
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)