1 Star 2 Fork 1

soda151314 / TestTreeModelEx

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
treemodel.h 2.28 KB
一键复制 编辑 原始数据 按行查看 历史
soda 提交于 2023-12-12 15:26 . 1.qml实现树形控件。
#pragma once
#include <memory>
#include <QAbstractItemModel>
#include <QModelIndex>
#include <QVariant>
#include <QQmlEngine>
#include <treedata.h>
Q_DECLARE_METATYPE(CusTreeNode)
QML_DECLARE_TYPE(CusTreeNode)
class TreeModel : public QAbstractItemModel
{
Q_OBJECT
public:
explicit TreeModel(QObject *parent = nullptr);
~TreeModel();
enum nodeRoles {
NODE_NAME = Qt::UserRole + 1,
NODE_PTR = Qt::UserRole + 2,
};
Q_ENUM(nodeRoles)
Q_INVOKABLE QVariant data(const QModelIndex &index, int role) const override;
bool setData(const QModelIndex &index, const QVariant &value, \
int role = Qt::EditRole) override;
Qt::ItemFlags flags(const QModelIndex &index) const override;
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;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
QHash<int, QByteArray> roleNames() const override;
////////////////业务相关///////////////////////////
// 释放所有数据
void freeModelData();
// 加载数据
Q_INVOKABLE void loadData();
static void declareQML();
// 开始重新加载model
void beginResetTreeModel();
// 结束重新加载
void endResetTreeModel();
// 刷新指定节点
Q_INVOKABLE void updateModelData();
// 删除刷新
void startDeletelModelData(const QModelIndex &parent, int first, int last);
void endDeleteModelData();
// 插入刷新
void startInsertModelData(const QModelIndex &parent, int first, int last);
void endInsertModelData();
private:
LPCusTreeNode createNodeByFile(const QString& fileName, const QString& TestData);
void resetModelData(LPCusTreeNode rootItem);
LPCusTreeNode m_emptyItem; // (-1,-1)的虚假节点
LPCusTreeNode m_rootItem; // (0, 0)的根节点
QList<LPCusTreeNode> m_listNodes; // 所有节点
};
1
https://gitee.com/soda151314/test-tree-model-ex.git
git@gitee.com:soda151314/test-tree-model-ex.git
soda151314
test-tree-model-ex
TestTreeModelEx
master

搜索帮助