PDA

View Full Version : how to use QSortFilterProxyModel with QtreeWidget?



patcito
6th July 2006, 21:24
I use QtreeWidget to load an xml xbel file using the sax example given with Qt4.
I would like to put a lineEdit and use it to filter my QtreeWidget like konqueror bookmark editor does (or amarok etc...).

Here is the code I use, following the documentation exemple:

QAbstractItemModel * sourceModel;
sourceModel = ui.treeWidget->model();

QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
proxyModel->setSourceModel(sourceModel);
ui.treeWidget->setModel(proxyModel);

proxyModel->setFilterRegExp(QRegExp(".png", Qt::CaseInsensitive,
QRegExp::FixedString));


The problem is that setModel doesn´t work that way with QtreeWidget, it seems to be private or something, here is the error I get on compile:

/usr/local/Trolltech/Qt-4.2.0-tp1/include/QtGui/qtreewidget.h:301: error: "virtual void QTreeWidget::setModel(QAbstractItemModel*)" is private
myapp.cpp:108: error: within this context
make: *** [myapp.o] Error 1

any idea how to do it right?

I have another question, the doc says about QSortFilterProxyModel
For hierarchical models, the filter is applied recursively to all children. If a parent item doesn't match the filter, none of its children will be shown. I would like all parent items to be shown as long as one of their children match the filter. How could I do that?

Thanx in advance

Pat

Jimmy2775
6th July 2006, 22:37
I believe that in order to be able to call setModel you will need to use a QTableView rather than a QTableWidget.

patcito
7th July 2006, 21:29
that´s ok thanx I found out. I had to use:


ui.treeWidget->QtreeWidget::setModel(proxyModel);

instead of


ui.treeWidget->setModel(proxyModel);

jpn
7th July 2006, 22:30
QTreeWidget has it's own strictly private model and it is not meant to be changed by hacking like you do (assuming that is QTreeView::setModel() you are calling). I doubt it would work very well.. QTreeWidget implementation for example does some casting which assumes that model indexes' internal pointers point to a QTreeWidgetItem. This is not true in your case when you have changed the underlying model. The methods like QTreeWidget::model() and QTreeWidget::setModel() have not been declared as private with no reason.

Use a QTreeView if you want to use a model rather than the item approach QTreeWidget offers!

adrianoleal
13th July 2011, 13:43
QTreeWidget has it's own strictly private model and it is not meant to be changed by hacking like you do (assuming that is QTreeView::setModel() you are calling). I doubt it would work very well.. QTreeWidget implementation for example does some casting which assumes that model indexes' internal pointers point to a QTreeWidgetItem. This is not true in your case when you have changed the underlying model. The methods like QTreeWidget::model() and QTreeWidget::setModel() have not been declared as private with no reason.

Use a QTreeView if you want to use a model rather than the item approach QTreeWidget offers!

And if i want to change the display text of the my files in QTreeView? Do you know how can i do? This is my big problem, right now...

With QTreeView i can to change the model to QFileSystemModel and in QTreeWidget i can't. But in QTreeWidget i can to change the text files display and in QTreeView i can't. And i need both, to change text files display and to use QFileSystemModel. =/

If can you help me...