QAbstractItemModel动态项插入

时间:2016-04-27 13:40:57

标签: c++ qt qml qabstractitemmodel

我为我的模型继承了QAbstractItemModel类。为了方便地将新项目插入到模型中,我编写了下一个方法:

void addItem(MyData *parent, MyData *children) {
    QModelIndex idx = createIndex(parent->row(), 0, parent);
    if (!idx.isValid()) {
        return;
    }

    int childCount = parent->getChildCount();
    beginInsertRows(idx, childCount, childCount);
    parent->addChild(children);
    endInsertRows();

    emit layoutChanged(QList<QPersistentModelIndex>{idx});
}

它适用于QListView,但QML的TreeView在它显示之后不会更新值:

int main(int argc, char ** argv) {
    Q_INIT_RESOURCE(ui);
    QApplication application(argc, argv);

    MyModel model;

    for (int i = 0; i < 10; ++ i) {
        MyData *firstLevelItem = new MyData(i);

        for (int k = 0; k < 3; ++ k) {
            MyData *secondLevelItem = new MyData(i);
            model.addItem(firstLevelItem, secondLevelItem);
        }

        model.addItem(model.getRootItem(), firstLevelItem);
    }

    QQuickView view;
    QQmlContext *context = view.rootContext();
    context->setContextProperty("MyModel", &model);

    view.setSource(QUrl("qrc:///ui/form.qml"));
    view.show();

    QTreeView t;
    t.setModel(&model);
    t.show();

    MyData *data = new MyData(2281488);
    model.addItem(model.getRootItem(), data);
    // t displays changes, view - not

    return application.exec();
}

MyData类:

class MyModel;

class MyData: public QObject {
    Q_OBJECT

public:
    explicit MyData() :
        QObject() {
        _parent = nullptr;
    }

    ~MyData() {
        qDeleteAll(_data);
    }

    // getters / setters

    MyData *getChildItem(int index) const {
        if (index < 0 || index >= _data.size()) {
            return nullptr;
        }

        return _data[index];
    }

    int getChildCount() const {
        return _data.size();
    }

    MyData *parent() const {
        return _parent;
    }

    int row() const {
        if (_parent) {
            return _parent->_data.indexOf(const_cast<MyData *>(this));
        } else {
            return 0;
        }
    }

private:
    void addChild(MyData *data) {
        if (data) {
            if (data->_parent) {
                _parent->removeChild(data);
            }
            data->_parent = this;
            _data << data;
        }
    }

    void removeChild(MyData *data) {
        _data.removeAll(data);
    }

    // some private fields

    MyData *_parent;

    QVector<MyData *> _data;

    friend class MyModel;
};

1 个答案:

答案 0 :(得分:0)

QML的ListView不适合显示类似目录树的树状结构。它仅适用于通常从QAbstractListModel继承的列表模型。如果从QAbstractItemModel派生模型,则必须对其进行专门化,使其行为类似于列表。

从Qt 5.5开始,模块QtQuick.Controls(v1.4)中有一个QML TreeView组件。你可能想试试这个。