Update QTreeView on add data to subitem



  • I have a QTreeView with data
    item 1
    subitem 1-1
    sub sub item 1-1-1
    sub sub item 1-1-2
    sub sub item 1-1-3
    item 2
    I have a model from QAbstractItemModel
    When I add new sub item 2-1 to item 2 my tree view update good, but when I try add data to item, whitch already have data (for example add subitem 1-2 to item 1) my view doesn't update (item is invisible in view tree). How I can correct this?
    I try use dataChanged, but I can use it for 1st level of tree only. I don't now how use it for subitem

    void ModelTreeObject::insertOrUpdate(QVector<TreeData*> &treeDatas)
    {
        foreach (TreeData* treeData, treeDatas) {        
            bool update = false;
            for(int i = 0; i < rootItem->childCount(); i++)
            {
                if(rootItem->child(i)->id() == treeData->id)//update
                {
    
                    rootItem->child(i)->setData(treeData);
                    update = true;
                    emit dataChanged(index(i,0), index(i,4));
                    break;
                }
            }
            if(!update)
            {
                beginInsertRows(QModelIndex(),rootItem->childCount(),rootItem->childCount());
                rootItem->insertChildren(rootItem->childCount(), treeData);
                endInsertRows();
    
            }
        }
    }
    
    void ModelTreeObject::remove(quint64 id)
    {
        for(int i = 0; i < rootItem->childCount(); i++)
        {
            if(rootItem->child(i)->id() == id)
            {
                beginRemoveRows(QModelIndex(),i,i);
                rootItem->removeChildren(i,1);
                endRemoveRows();
                break;
            }
        }
    }
    


  • @klynxe said in Update QTreeView on add data to subitem:

    emit dataChanged(index(i,0), index(i,4));

    I could be wrong ( :) ), but where do you get that index() from? QModelIndex() takes a parent, which is needed for non-top-level items. In a tree view I think you must use for sub-items?



  • @klynxe said in Update QTreeView on add data to subitem:

    emit dataChanged(index(i,0), index(i,4));

    You are passing the wrong a indexes. Those are 4 columns in the top level, you should pass 1 more argument specifying the parent to each index() call


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.