Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QTreeView & QAbstractItemModel & insertRow

I'm trying to implement QAbstractItemModel for QTreeView. I have problem with inserting rows. I noticed that if I insert at the beginning of my application all works fine. But If I insert rows later - after some other operations (like selections etc.) new items stay invisible. Moreover QTreeView seems to doesn't work at all! Do I have to emit some signals to notify QTreeView about rows insertion?

This is my insertion method:

bool LayersModel::insertRows(int position, int count, const QModelIndex  & parent)
{
    LayersModelItem * parentItem = getItem(parent);
    if (position > parentItem->childCount())
        return false;
    beginInsertRows(parent,position,position+count-1);
    bool result = true;
    for (;count;--count)
        result &= parentItem->insertChildren(position, new LayersModelItem());
    endInsertRows();
    return result;
}

LayersModelItem is class with QList with its children and data.

Full code of my project (KDE libs needed) is here: https://github.com/coder89/PhotoFramesEditor/tree/v0.0.8 To see the problem select one of blue item on main window and then right-click on it and select "Delete item". (this method is in Canvas::removeItems()) and it is completly commented - I'm desperate and I've tried to find reason of this problem... (in fact it wasn't delete anything - it adds new item).

Thanks for any help & advice!

like image 881
Lukasz Spas Avatar asked Jul 05 '11 17:07

Lukasz Spas


1 Answers

Just a quick guess, the QT Doc for QAbstractItemModel says...

The model emits signals to indicate changes. For example, dataChanged() is emitted whenever items of data made available by the model are changed. Changes to the headers supplied by the model cause headerDataChanged() to be emitted. If the structure of the underlying data changes, the model can emit *layoutChanged() to indicate to any attached views that they should redisplay any items shown, taking the new structure into account*.

So i guess, you need to emit layoutChanged() signal from your model (whenever you change the data in model) in order to update connected views.

Also read the QT docs for model view architecture, how it is implemented in QT

see if that helps, if it doesn't i will try to download your code and debug it and see, what's wrong. Good Luck

like image 116
maxchirag Avatar answered Sep 18 '22 13:09

maxchirag