首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >QAbstractItemModel顶层的QML委托

QAbstractItemModel顶层的QML委托
EN

Stack Overflow用户
提问于 2018-04-02 07:46:54
回答 1查看 443关注 0票数 0

我有一个双层的QAbstractItemModel-derived自定义树模型,我想在QML中显示它。是否有办法为模型的每一层分配不同的委托?

附注:首选解决方案意味着对模型没有任何更改。

编辑:您可以假设模型看起来有点像这样:

代码语言:javascript
复制
// testmodel.h

#ifndef TESTMODEL_H
#define TESTMODEL_H

#include <QAbstractItemModel>

class TestModel : public QAbstractItemModel
{
    Q_OBJECT
public:
    QModelIndex index(int row, int column, const QModelIndex &parent = {}) const;
    QModelIndex parent(const QModelIndex &child) const;
    QModelIndex sibling(int row, int column, const QModelIndex &index) const;

    int rowCount(const QModelIndex &index = {}) const;
    int columnCount(const QModelIndex & = {}) const { return 1; }

    QVariant data(const QModelIndex &, int) const { return {}; }
    bool setData(const QModelIndex &, const QVariant &, int) { return false; }

    bool insertRows(int from, int count, const QModelIndex &parent);
    bool removeRows(int from, int count, const QModelIndex &parent);

private:
    QVector<int> _sizes;
};

#endif // TESTMODEL_H


// testmodel.cpp

#include "testmodel.h"

constexpr uintmax_t defId = -1;

QModelIndex TestModel::index(int row, int column, const QModelIndex &parent) const {
    return createIndex(row, column, parent.isValid() ? parent.row() : defId);
}

QModelIndex TestModel::parent(const QModelIndex &child) const {
    auto id = child.internalId();
    if (id == defId)
        return {};
    return createIndex(id, 0, defId);
}

QModelIndex TestModel::sibling(int row, int column, const QModelIndex &index) const {
    if (!index.isValid())
        return {};
    return createIndex(row, column, index.internalId());
}

int TestModel::rowCount(const QModelIndex &index) const {
    if (!index.isValid())
        return _sizes.size();

    if (index.internalId() != defId)
        return 0;

    int row = index.row();
    return row >= _sizes.size() ? 0 : _sizes[index.row()];
}

bool TestModel::insertRows(int from, int count, const QModelIndex &parent) {
    if (count <= 0 || from < 0)
        return false;

    auto to = from + count - 1;

    if (!parent.isValid()) {
        if (from > _sizes.size())
            return false;

        beginInsertRows(parent, from, to);
        _sizes.insert(from, count, 0);
        endInsertRows();
        return true;
    }

    if (parent.internalId() != defId)
        return false;

    int row = parent.row();
    if (row >= _sizes.size())
        return false;

    if (from > _sizes[row])
        return false;

    beginInsertRows(parent, from, to);
    _sizes[row] += count;
    endInsertRows();
    return true;
}

bool TestModel::removeRows(int from, int count, const QModelIndex &parent) {
    if (count <= 0 || from < 0)
        return false;

    auto to = from + count - 1;

    if (!parent.isValid()) {
        if (to >= _sizes.size())
            return false;
        beginRemoveRows(parent, from, to);
        _sizes.remove(from, count);
        endRemoveRows();
        return true;
    }

    if (parent.internalId() != defId)
        return false;

    int row = parent.row();
    if (row >= _sizes.size())
        return false;

    if (to >= _sizes[row])
        return false;

    beginRemoveRows(parent, from, to);
    _sizes[row] -= count;
    endRemoveRows();
    return true;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-04 04:09:27

事实上,我正在寻找DelegateModel组件(或VisualDataModel)。列出一个简单的示例,该示例将模型的顶层显示为红色行,从底层填充蓝色矩形:

代码语言:javascript
复制
ListView {
    id: view
    anchors.fill: parent

    model: DelegateModel {
        model: testModel // testModel is a tree model as context property

        delegate: Rectangle {
            width: win.width
            height: 30
            color: "red"
            border.color: "black"

            ListView {
                anchors.fill: parent
                orientation: Qt.Horizontal

                model: DelegateModel {
                    model: testModel
                    rootIndex: view.model.modelIndex(index, 0)

                    delegate: Rectangle {
                        height: 30
                        width: height
                        color: "blue"
                        border.color: "black"
                    }
                }
            }
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49607323

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档