Meteo ir3
Meteo ir3

Reputation: 449

QSortFilterProxyModel how to handle QStandardItems correctly

I have QTreeView with some items and search QLineEdit with connected slot on textEdited signal. With this code:

QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
proxyModel->setSourceModel(messagesModel);
proxyModel->setFilterFixedString(text);
ui.treeView->setModel(proxyModel);

text filtering is ok, but when I clicked on QTreeView QStandardItems checkboxes (after proxy model assigned to QTreeView), I have the program crashes in slot, that connected to this QTreeView original model (before proxy was assigned). What is the right way to processing item checkbox clicks? Need I use new connect/slot to processing model changes, or I can use the same code for original model with some changes? I just need to hide filtered items in QTreeView. In QTreeWidget is hide() method, does QTreeView has something like this, or QSortFilterProxyModel - is what I need? Thx!

UPD crashed in slot, connected to treeView:

auto item = messagesModel->itemFromIndex(index); // item is NULL because proxyModel is set for TreeView now
if(item->whatsThis().isEmpty()) return;    // error below

#ifndef QT_NO_WHATSTHIS
    inline QString whatsThis() const {
        return qvariant_cast<QString>(data(Qt::WhatsThisRole));
    }
    inline void setWhatsThis(const QString &whatsThis);
#endif

because I set proxyModel to treeView, but messagesModel have whatsThis...

Upvotes: 0

Views: 599

Answers (1)

Meteo ir3
Meteo ir3

Reputation: 449

I changed my code with that:

QStandardItem* item;
if(ui.leFilter->text().isEmpty())
    item = messagesModel->itemFromIndex(index);
else
    item = messagesModel->itemFromIndex(proxyModel->mapToSource(index));
if(item->whatsThis().isEmpty()) return;

and it works. Is that correct way? Proxy model is member of my UI class ... not local.

UPD how can I update source model when checkbox checked in proxyModel?

UPD 2 I have load "original" model for QtreeView and show it. When I edit text in QListEdit, I use proxyModel (code from 1st post). When text edited, I have check checkboxes in QtreeView (now proxyModel is active) and at this step all is ok. But when I do some changes in UI, in QTreeView set the original model and it has no changes that was made for proxyModel. How can I notify and update items in source Model with new data from proxyModel?

UPD3 Yes, source model is also modified ... I have just clear it)

Upvotes: 0

Related Questions