• 2171阅读
  • 0回复

[提问]集成了QAbstractItemModel自己实现树,叶子节点不显示 [复制链接]

上一主题 下一主题
离线nctqzhukw
 

只看楼主 正序阅读 楼主  发表于: 2019-06-12

TreeModel.h

#pragma once

#include <QAbstractItemModel>
#include <memory>
#include <set>


struct Tree_Group_Struct;


struct Tree_Member_Struct
{
    QString strMemberName = "";
    std::weak_ptr<struct Tree_Group_Struct> srtGroup;
};
Q_DECLARE_METATYPE(std::shared_ptr<struct Tree_Member_Struct>)


class TreeMemberSort
{
public:
    bool operator()(std::shared_ptr<struct Tree_Member_Struct> srtMemberOne, std::shared_ptr<struct Tree_Member_Struct> srtMemberTwo)
    {
        if (!srtMemberOne || !srtMemberTwo)
        {
            return false;
        }
        if (srtMemberOne->strMemberName > srtMemberTwo->strMemberName)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
};
struct Tree_Group_Struct
{
    QString strGroupName = "";
    bool bIsExtend = true;
    std::vector<std::shared_ptr<struct Tree_Member_Struct>> strMember;
    std::multiset<std::shared_ptr<struct Tree_Member_Struct>, TreeMemberSort> setMember;
};
Q_DECLARE_METATYPE(std::shared_ptr<struct Tree_Group_Struct>)



class TreeModel : public QAbstractItemModel
{
    Q_OBJECT

public:
    TreeModel(QObject *parent = nullptr);
    ~TreeModel();

    virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;


    virtual QModelIndex parent(const QModelIndex &child) const override;


    virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override;


    virtual int columnCount(const QModelIndex &parent = QModelIndex()) const override;


    virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
private:
    std::vector<std::shared_ptr<struct Tree_Group_Struct>> m_listStr;
};

TreeModel.cpp
#include "TreeModel.h"

TreeModel::TreeModel(QObject *parent)
    : QAbstractItemModel(parent)
{
    std::shared_ptr<struct Tree_Group_Struct> loGroup = std::make_shared<struct Tree_Group_Struct>();
    loGroup->strGroupName = "兴趣组";
    std::shared_ptr<struct Tree_Member_Struct> ptr = std::make_shared<struct Tree_Member_Struct>();// Tree_Member_Struct{ "兴趣1",loGroup }
    ptr->strMemberName = "zhukw";
    ptr->srtGroup = loGroup;
    loGroup->strMember.push_back(ptr);
    //loGroup->strMember.push_back(std::make_shared<struct Tree_Member_Struct>(Tree_Member_Struct{ "兴趣2" ,loGroup }));
    //loGroup->strMember.push_back(std::make_shared<struct Tree_Member_Struct>(Tree_Member_Struct{ "兴趣3" ,loGroup }));
    m_listStr.push_back(std::move(loGroup));
    /*
    std::shared_ptr<struct Tree_Group_Struct> loGroup2 = std::make_shared<struct Tree_Group_Struct>();
    loGroup2->strGroupName = "爱好组";
    for (int i = 0; i < 1; i++)
    {
        QString str = QString("爱好%1").arg(i);
        loGroup2->strMember.push_back(std::make_shared<struct Tree_Member_Struct>(Tree_Member_Struct{ QString(str) ,loGroup2 }));
    }

    m_listStr.push_back(std::move(loGroup2));
    */

    /*
    std::shared_ptr<struct Tree_Group_Struct> loGroup3 = std::make_shared<struct Tree_Group_Struct>();
    loGroup3->strGroupName = "三号组";
    for (int i = 0; i < 1; i++)
    {
        QString str = QString("爱好%1").arg(i);
        loGroup3->strMember.push_back(std::make_shared<struct Tree_Member_Struct>(Tree_Member_Struct{ QString(str) ,loGroup3 }));
    }

    m_listStr.push_back(std::move(loGroup3));
    */
}

TreeModel::~TreeModel()
{
}

QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent /*= QModelIndex()*/) const
{
    if (!parent.isValid())
    {
        if (row < m_listStr.size())
        {
            return createIndex(row, column, m_listStr.at(row).get());
        }
    }

    bool bIsFind = false;
    int i;
    for (i = 0; i < m_listStr.size(); i++)
    {
        if (m_listStr.at(i).get() == parent.internalPointer())
        {
            bIsFind = true;
            break;
        }
    }
    if (bIsFind)
    {
        if (row < m_listStr.at(i)->strMember.size())
        {
            return createIndex(row, column, m_listStr.at(i)->strMember.at(row).get());
        }
    }
    return QModelIndex();
}

QModelIndex TreeModel::parent(const QModelIndex &child) const
{
    bool bIsFind = false;
    for (int i = 0; i < m_listStr.size(); i++)
    {
        if (m_listStr.at(i).get() == child.internalPointer())
        {
            return QModelIndex();
        }
        for (int j = 0; j < m_listStr.at(i)->strMember.size(); j++)
        {
            if (m_listStr.at(i)->strMember.at(j).get() == child.internalPointer())
            {
                return createIndex(i, 0, m_listStr.at(i).get());
            }
        }
    }
    return QModelIndex();
}

int TreeModel::rowCount(const QModelIndex &parent /*= QModelIndex()*/) const
{
    if (!parent.isValid())
    {
        return m_listStr.size();
    }
    else
    {
        if (parent.row() < m_listStr.size())
        {
            return m_listStr.at(parent.row())->setMember.size();
        }
        else
        {
            return 0;
        }
    }
    return 0;
}

int TreeModel::columnCount(const QModelIndex &parent /*= QModelIndex()*/) const
{
    return 1;
}

QVariant TreeModel::data(const QModelIndex &index, int role /*= Qt::DisplayRole*/) const
{
    if (!index.isValid())
    {
        return QVariant();
    }
    short nIsFind = 0;
    int i, j;
    for (i = 0; i < m_listStr.size(); i++)
    {
        if (m_listStr.at(i).get() == index.internalPointer())
        {
            nIsFind = 1;
            break;
        }
        for (j = 0; j < m_listStr.at(i)->strMember.size(); j++)
        {
            if (m_listStr.at(i)->strMember.at(j).get() == index.internalPointer())
            {
                nIsFind = 2;
                break;
            }
        }
        if (nIsFind == 2)
        {
            break;
        }
    }
    if (!nIsFind)
    {
        return QVariant();
    }

    switch (role)
    {
    case Qt::DisplayRole:
    {
        if (nIsFind == 1)
        {
            if (index.row() < m_listStr.size())
            {
                return QVariant(m_listStr.at(index.row())->strGroupName);
            }
        }
        else if (nIsFind == 2)
        {
            if (index.row() < m_listStr.at(i)->strMember.size())
            {
                return QVariant(m_listStr.at(i)->strMember.at(index.row())->strMemberName);
            }
        }
    }
        break;
    }
    return QVariant();
}


快速回复
限100 字节
 
上一个 下一个