Veaceslav
Veaceslav

Reputation: 51

QAbstractModel - can't get QAbstractModelItem with getItem an QModelIndex

Hello I tried to implement a custom QAbstractModel to use it on QtreeView.

Main requirement was to store as a tree QDomNodes so i can easily access/delete/add childrens.

But in this method i receive a segmentation fault

ProjectTreeItem *ProjectTreeModel::getItem(const QModelIndex &index) const
{
    if (index.isValid()) {
         ProjectTreeItem *item = static_cast<ProjectTreeItem*>(index.internalPointer());
         if (item) return item;
     }
     return rootItem;
}

here is entire file:

http://pastebin.com/HmWZwVmC - projecttreemodel.cpp

http://pastebin.com/4nDXDVX0 - projecttreeitem.cpp

here what i try to do:

void Ide::slotDeleteItem()
{
    /**
      * ui->projectsView is a QTreeView with setModel(model)
          * model is a ProjectTreeModel
          */
    QItemSelectionModel* sel = ui->projectsView->selectionModel();

    QModelIndexList lst = sel->selectedIndexes();

    QModelIndex ind = lst.at(0);

    ProjectTreeItem* item = model->getItem(ind);

/** SEGFAULT even if getItem is moved to public(default is private) **/
    qDebug() << item->data(Qt::DisplayRole).toString();
/** SEGFAULT **/
    qDebug() << model->data(ind,Qt::DisplayRole);

/** Works and display information correct, but i need to access to ProjectTreeItem **/

    qdebug() << ind.data(Qt::DisplayRole);
}

i'm not sure about what "internal pointer" stuff is doing, if anyone can help, please ?

thanks!

Upvotes: 1

Views: 639

Answers (1)

Veaceslav
Veaceslav

Reputation: 51

I found the source of the problem, it wasnțt de model problem, I used a QProxyModel to filter some nodes and all QModelIndex provided were invalid. Do not use QproxyModel if you want to use selectionModel to get selected indexes.

Upvotes: 1

Related Questions