先在qt的ui界面拖一个treeview
tree_model = new MissionTreeModel(this);
ui->treeViewMissions->setModel(tree_model);
ui->treeViewMissions->setSelectionBehavior(QTreeView::SelectRows);
ui->treeViewMissions->setSelectionMode(QTreeView::SingleSelection);
ui->treeViewMissions->setFocusPolicy(Qt::NoFocus);
ui->treeViewMissions->setContextMenuPolicy(Qt::CustomContextMenu);
// ui->treeViewMissions->header()->setStyleSheet("QHeaderView::section{background:#3a3e46;color: white;}");
TreeItem.h
#ifndef TREEITEM_H
#define TREEITEM_H
#include<QtCore>
class TreeItem
{
public:
TreeItem();
~TreeItem();
QVariant getData() const;
bool setData(const QVariant &value);
bool setParent(TreeItem *node);
//得到当前节点孩子的数量
int getChildrenCount() const;
void addChildren(TreeItem *node);
TreeItem*getChild(int row);
TreeItem *getPrent() const;
//得到当前项的索引
int getCurrentRow();
private:
QVariant m_data;
QList<TreeItem*> m_children;
TreeItem* m_prent = nullptr;
};
#endif // TREEITEM_H
MissionTreeModel.h
#ifndef MISSIONTREEMODEL_H
#define MISSIONTREEMODEL_H
#include <QAbstractItemModel>
#include "treeitem.h"
class MissionTreeModel : public QAbstractItemModel
{
Q_OBJECT
public:
MissionTreeModel(QObject *parent = nullptr);
// Header:
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
// 索引结构
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
QModelIndex parent(const QModelIndex &index) const override;
//节点的行列(m,n)
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
//显示数据
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
//是否可编辑
Qt::ItemFlags flags(const QModelIndex &index) const override;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
TreeItem *itemFromIndex(const QModelIndex &index) const;
bool add_mission(QString plane_type, QString mission_name);
bool delete_mission(QString plane_type, QString mission_name);
void refresh();
private:
TreeItem*m_rootNode=nullptr;
QStringList headers;
};
#endif // MISSIONTREEMODEL_H
TreeItem.cpp
#include "treeitem.h"
TreeItem::TreeItem()
{
}
TreeItem::~TreeItem()
{
for(int i = 0; i < m_children.size(); i++)
delete m_children[i];
m_children.clear();
}
QVariant TreeItem::getData() const
{
return m_data;
}
bool TreeItem::setData(const QVariant &value)
{
if(value.isNull()||value=="")return false;
if(value.isValid())
{
m_data = value;
return true;
}
return false;
}
bool TreeItem::setParent(TreeItem *node)
{
m_prent = node;
return true;
}
int TreeItem::getChildrenCount() const
{
return m_children.count();
}
void TreeItem::addChildren(TreeItem *node)
{
m_children.append(node);
}
TreeItem *TreeItem::getChild(int row)
{
return m_children.at(row);
}
TreeItem *TreeItem::getPrent() const
{
return m_prent;
}
int TreeItem::getCurrentRow()
{
if(m_prent)
return m_prent->m_children.indexOf(const_cast<TreeItem*>(this));
return 0;
}
MissionTreeModel.cpp
#include "missiontreemodel.h"
#include "global.h"
#include "treedata.h"
MissionTreeModel::MissionTreeModel(QObject *parent) : QAbstractItemModel(parent)
{
m_rootNode = TreeData::getInstence()->getRootNode();
headers << QStringLiteral("机型") << QStringLiteral("任务");
}
QVariant MissionTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
return headers[section];
return QVariant();
}
QModelIndex MissionTreeModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
TreeItem *parentItem = itemFromIndex(parent);
TreeItem *item = parentItem->getChild(row);
if (item)
return createIndex(row, column, item);
else
return QModelIndex();
}
QModelIndex MissionTreeModel::parent(const QModelIndex &index) const
{
if (!index.isValid())
return QModelIndex();
TreeItem *item = itemFromIndex(index);
TreeItem *parentItem = item->getPrent();
if (parentItem == m_rootNode)
return QModelIndex();
return createIndex(parentItem->getChildrenCount(), 0, parentItem);
}
int MissionTreeModel::rowCount(const QModelIndex &parent) const
{
if (parent.column() > 0)
return 0;
TreeItem* item = itemFromIndex(parent);
return item->getChildrenCount();
}
int MissionTreeModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return 1;
}
QVariant MissionTreeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role != Qt::DisplayRole)
return QVariant();
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
return item->getData();
}
Qt::ItemFlags MissionTreeModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return 0;
return QAbstractItemModel::flags(index);
//return Qt::ItemIsEditable | QAbstractItemModel::flags(index);
}
bool MissionTreeModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (role != Qt::EditRole)
return false;
if(!index.isValid())
return false;
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
bool result =item->setData(value);
// if (result)
// emit dataChanged(index, index);
return result;
}
TreeItem *MissionTreeModel::itemFromIndex(const QModelIndex &index) const
{
if (index.isValid())
{
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
return item;
}
return m_rootNode;
}
bool MissionTreeModel::add_mission(QString plane_type, QString mission_name)
{
bool ok = TreeData::getInstence()->add_mission(plane_type, mission_name);
if(!ok)
return false;
m_rootNode = TreeData::getInstence()->getRootNode();
return true;
}
bool MissionTreeModel::delete_mission(QString plane_type, QString mission_name)
{
bool ok = TreeData::getInstence()->delete_mission(plane_type, mission_name);
if(!ok)
return false;
m_rootNode = TreeData::getInstence()->getRootNode();
return true;
}
void MissionTreeModel::refresh()
{
beginResetModel();
//...
endResetModel();
}