QTreeView普通数组模型

时间:2016-06-07 15:08:36

标签: c++ qt qt5

我有一个非常大的数据数组。我想使用QTreeView和模型/视图方法来显示在具有特定参数的树节点中分组的数据,例如。数据值< 10,数据值< 100,数据值< 1000.

官方树模型示例显示如何对项目节点使用分层数据结构,这对我来说是不好的选择。

我试图从QAbstractItemModel自己编写模型,但我甚至不知道如何为组节点(< 10,< 100,< 1000)编写parent()方法了例如,子节点。

是否可以编写这样的模型?

1 个答案:

答案 0 :(得分:1)

您需要创建“人工”父项,它们是没有值的模型索引,但只对应于数据值大于< 100。

每个项目的数据值< 100将指代同一父母。当被要求为其子女(以及申请了rowCount()的孩子的数量)时,这位家长需要提供所有这些项目。

bool MyModel::isCategory(const QModelIndex &index) const
{
    /* Based on your internal tracking of indexes, return if this is 
       a category (with children), false if not */
}

int MyModel::rowCount(const QModelIndex & parent) const
{
    if (!parent.isValid()) {
        /* This is top level */
        /* Return the number of different categories at top level */
    }
    if (isCategory(parent)) {
        /* This is a parent category (for example items with data value < 100 */
        /* Return the number of indexes under it */
    }
    /* This is a child element with just data in it, no children */
    return 0;
}

QModelIndex MyModel::index(int row, int column, const QModelIndex & parent) const
{
    if (!parent.isValid()) {
        /* Return QModelIndex corresponding to the nth global category, with n = row */
    }
    /* return the nth sub-element of the category represented by parent, with n = row */
}

QModelIndex QAbstractItemModel::parent(const QModelIndex & index) const
{
    /* If index is a top level category, return an empty QModelIndex */
    /* Otherwise return the QModelIndex corresponding to the bigger category */
}

在一个简化的案例中提供一个最小的代码示例,如果这仍然不够,则代码不能正常工作。