QML model/view 模型实现TreeView显示

1.通过c++代码实现model,继承自QAbstractItemModel,代码如下:

①头文件实现如下(CustomTreeModel.h):

#ifndef CUSTOMTREEMODEL_H
#define CUSTOMTREEMODEL_H

#include <QAbstractItemModel>
#include "TreeItem.h"

class CustomTreeModel : public QAbstractItemModel
{
    Q_OBJECT

public:
    explicit CustomTreeModel(const QString &data="main1\tsub\n child1\tsub\nmain2\tsub2", QObject *parent = 0);
    ~CustomTreeModel();

    QVariant data(const QModelIndex &index, int role) const 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> CustomTreeModel::roleNames() const override;

private:
    void setupModelData(const QStringList &lines, TreeItem *parent);

    TreeItem *rootItem;
    int NAME;
    int SUMMARY;
};

#endif // CUSTOMTREEMODEL_H

②源文件实现如下(CustomTreeModel.cpp):

#include "CustomTreeModel.h"
#include <QStringList>
#include <QDebug>

CustomTreeModel::CustomTreeModel(const QString &data, QObject *parent)
     : QAbstractItemModel(parent)
{
    NAME = Qt::UserRole;
    S
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要自定义C++ model 实现 QMLTreeView,需要遵循以下步骤: 1. 创建一个 C++ 类,继承自 QAbstractItemModel。 2. 实现必要的虚函数,包括 rowCount、columnCount、parent、index、data 等。 3. 在 model 中创建树形结构。 4. 在 QML 中创建 TreeView 控件,并绑定数据源为自定义的 C++ model。 5. 为 TreeView 控件定义 itemDelegate,以便在 TreeView显示自定义的数据。 以下是一个简单的示例: C++ model: ```c++ class TreeModel : public QAbstractItemModel { Q_OBJECT public: explicit TreeModel(QObject *parent = nullptr); ~TreeModel(); // 实现必要的虚函数 QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; QModelIndex parent(const QModelIndex &child) const override; 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; private: // 存储树形结构的数据 QList<TreeItem*> m_items; }; ``` QML TreeView: ```qml TreeView { model: myModel itemDelegate: Rectangle { width: 200 height: 30 Text { text: model.name // 自定义数据 anchors.centerIn: parent } } } ``` 在 TreeModel 中,需要实现一个 TreeItem 类来存储树形结构的数据。在 data 函数中,可以根据需要返回不同的数据,例如 name、icon 等。在 QML 中,可以通过 itemDelegate 自定义每个 item 的显示方式。 需要注意的是,自定义的 C++ modelQML 中使用时需要在 main 函数中注册。可以使用 qmlRegisterType 函数将自定义的 C++ 类注册到 QML 中,例如: ```c++ qmlRegisterType<TreeModel>("MyModel", 1, 0, "TreeModel"); ``` 这样就可以在 QML 中使用 TreeModel 作为数据源了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值