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
I believe you want to do something like:
void myclass::clickAction(const QModelIndex &index)
{
QStandardItem *item = mymodel->itemFromIndex(mysort->mapToSource(index));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With