Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use itemFromIndex in clicked signal of QTreeView with QSortFilterProxyModel

Tags:

qt

I have a QTreeView with a QSortFilterProxyModel between the view and a QStandardItemModel to sort the tree. I then want to act on clicks in the view through the clicked() signal.

The models/view are setup similar to this:

mymodel  = new QStandardItemModel(5, 5, this);
mysort = new MySortProxy(this);
mysort->setSourceModel(mymodel);
myview = new QTableView(this);
myview->setSourceModel(mysort);
connect(myview, SIGNAL(clicked(QModelIndex)), this, slot(clickAction(QModelIndex)));

This setup all works and sorts my data in the way I want it. When you click on an item, the clickAction() slot gets called with the index of the item clicked. I then try to get the item from the index in the slot:

void myclass::clickAction(const QModelIndex &index)
{
    QStandardItem *item = mymodel->itemFromIndex(index);
}

However, itemFromIndex returns NULL.

If I remove the QSortFilterProxyModel and set the model directly as sourcemodel in the view, it all works perfectly. I.e.

myview->setSourceModel(mymodel);    // was setSourceModel(mysort);

mymodel->itemFromIndex(index) now returns the item as expected, but obviously now I can't use my own sort proxy.

Can anyone tell me what I'm doing wrong and how I can get the item in the click slot when I have a sortfilter proxy in place?

I'm using Qt-4.3.1.

Thanks for any help, Giles

like image 701
giles123 Avatar asked Aug 27 '10 17:08

giles123


1 Answers

I believe you want to do something like:

void myclass::clickAction(const QModelIndex &index)
{
    QStandardItem *item = mymodel->itemFromIndex(mysort->mapToSource(index));
}
like image 103
Arnold Spence Avatar answered Sep 23 '22 12:09

Arnold Spence