TreeView 和C++ QAbstractItemModel 使用出现QML Item: Binding loop detected for property "width"



  • TreeView 代码:
    import QtQuick 2.5
    import QtQuick.Window 2.2
    import QtQml.Models 2.2
    import QtQuick.Controls 1.4

    ApplicationWindow {
    id:root;
    visible: true
    width: 800
    height: 600
    title: qsTr("文件管理器");

    ItemSelectionModel{
        id:sel;
        model: DirModel;
    }
    
    //
    //左侧是treeview
    //
    MyTreeView{
        id:tree;
        anchors.left: parent.left;
        anchors.top:parent.top;
        anchors.bottom: parent.bottom;
        //model: DirModel;
        selection: sel;
    }
    MyTableView{
        id:table;
        anchors.right:parent.right;
        anchors.left: tree.right;
        anchors.top:parent.top;
        anchors.bottom: parent.bottom;
    }
    

    }

    main.cpp 代码:
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    /* 注册dirmodel到元对象系统 */
    qmlRegisterUncreatableType<DirItemModel>("ma.user.filemgr",1,0,"DirItemModel",
                                             "this model cannot be created");
    DirItemModel *dirModel = new DirItemModel(&engine);
    engine.rootContext()->setContextProperty("DirModel",dirModel);
    //engine.rootContext()->setContextProperty("RootIndex",dirModel->rootIndex());
    engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
    
    return app.exec();
    

    //DirModel代码
    //
    //构造方法
    //
    DirItemModel::DirItemModel(QObject *parent)
    : QAbstractItemModel(parent)
    {
    QList<QVariant> list;
    list << QVariant(tr("我的电脑"));
    rootItem = new TreeItem(list);
    QStringList strList;
    strList << tr("C盘") << tr("D盘")<< tr("E盘");
    for(int i = 0; i < strList.count(); i++){
    QList <QVariant> list1;
    list1 << QVariant(strList.at(i));
    rootItem->appendChildItem(new TreeItem(list1,rootItem));
    }
    }

    DirItemModel::~DirItemModel()
    {
    delete rootItem;
    }

    QModelIndex DirItemModel::rootIndex()
    {
    return createIndex(0,0,rootItem);
    }

    QModelIndex DirItemModel::index(int row, int column, const QModelIndex &parent) const
    {
    if(!hasIndex(row,column,parent))
    return QModelIndex();

    TreeItem *parentItem;
    if(!parent.isValid()){
        parentItem = rootItem;
    }
    else{
        parentItem = static_cast<TreeItem *>(parent.internalPointer());
    }
    
    TreeItem *childItem = parentItem->childItem(row);
    if(childItem){
        return createIndex(row,column,childItem);
    }
    else{
        return QModelIndex();
    }
    

    }

    QModelIndex DirItemModel::parent(const QModelIndex &child) const
    {
    if(!child.isValid()){
    return QModelIndex();
    }

    TreeItem *item = static_cast<TreeItem *>(child.internalPointer());
    TreeItem *parentItem = item->parentItem();
    

    // if(parentItem == rootItem){
    // return QModelIndex();
    // }
    return createIndex(parentItem->row(),0,parentItem);
    }

    int DirItemModel::rowCount(const QModelIndex &parent) const
    {
    TreeItem *parentItem;
    if(parent.column() > 0){
    return 0;
    }
    if(!parent.isValid()){
    parentItem = rootItem;
    }
    else{
    parentItem = static_cast<TreeItem *>(parent.internalPointer());
    }
    return parentItem->childrenCount();
    }

    int DirItemModel::columnCount(const QModelIndex &parent) const
    {
    if (parent.isValid())
    return static_cast<TreeItem*>(parent.internalPointer())->dataColumnCount();
    else
    return rootItem->dataColumnCount();
    }

    QVariant DirItemModel::data(const QModelIndex &index, int role) const
    {
    if (index.isValid() && role >= DIR_NAME) {
    switch (role) {
    case DIR_NAME:
    {
    if(!index.isValid()){
    qDebug()<<"!index.isValid()";
    return QVariant();
    }

            TreeItem *item = static_cast<TreeItem *>(index.internalPointer());
            if(item == rootItem){
                qDebug()<<"item == rootItem";
            }
            return QVariant(item->itemData(index.column()));
        }
        default:
            break;
        }
    }
    return QVariant();
    

    }

    QHash<int, QByteArray> DirItemModel::roleNames() const
    {
    QHash<int, QByteArray> result = QAbstractItemModel::roleNames();
    result.insert(DIR_NAME, QByteArrayLiteral("DirName"));
    return result;
    }

    实在不知问题在哪,应该根model有关把? //model: DirModel;注释掉就好了



  • @vvLinker 这做的是可编辑的 Model 吗?

    估计是QML的循环绑定,而不是 Model 的。

    另外可以学习一下 markdown,把你的代码格式一下。



  • @qyvlik 好的!



  • @vvLinker MyTreeView类的实现代码你贴一下吧。


 

最近的回复

关注我们

微博
QQ群











召唤伊斯特瓦尔