tags:

views:

26

answers:

1

Hi,

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

+2  A: 

I believe you want to do something like:

void myclass::clickAction(const QModelIndex &index)
{
    QStandardItem *item = mymodel->itemFromIndex(mysort->mapToSource(index));
}
Arnold Spence
Thanks a lot for your answer. Unfortunately I can't try this until Tuesday now as Monday is a bank holiday in England. However, your answer rings a lot of bells and I'm sure will prove to be correct so I will mark it as accepted now. I'll update this after I've tried it on Tuesday. Thanks.
gillez
Have tried this now and it worked a treat. Thanks.
gillez