I faced the same problem few month ago, I solve it by using the folowing class :
songtreeproxyfilter.h :
#ifndef SONGTREEPROXYFILTER_H
#define SONGTREEPROXYFILTER_H
#include <QSortFilterProxyModel>
{
public:
SongTreeProxyFilter
(QObject *parent
= NULL);
bool filterAcceptsRow
(int sourceRow,
const QModelIndex &sourceParent
) const;
private :
};
#endif // SONGTREEPROXYFILTER_H
#ifndef SONGTREEPROXYFILTER_H
#define SONGTREEPROXYFILTER_H
#include <QSortFilterProxyModel>
class SongTreeProxyFilter : public QSortFilterProxyModel
{
public:
SongTreeProxyFilter(QObject *parent = NULL);
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
private :
bool hasToBeDisplayed(const QModelIndex index) const;
};
#endif // SONGTREEPROXYFILTER_H
To copy to clipboard, switch view to plain text mode
songtreeproxyfilet.c
#include "songtreeproxyfilter.h"
#include <QtDebug>
SongTreeProxyFilter
::SongTreeProxyFilter(QObject *parent
):{
}
bool SongTreeProxyFilter::filterAcceptsRow(int sourceRow,
{
QModelIndex index
= sourceModel
()->index
(sourceRow,
0, sourceParent
);
return hasToBeDisplayed(index);
}
bool SongTreeProxyFilter
::hasToBeDisplayed(const QModelIndex index
) const {
bool result = false;
// How many child this element have
if ( sourceModel()->rowCount(index) > 0 )
{
for( int ii = 0; ii < sourceModel()->rowCount(index); ii++)
{
QModelIndex childIndex
= sourceModel
()->index
(ii,
0,index
);
if ( ! childIndex.isValid() )
break;
result = hasToBeDisplayed(childIndex);
if (result)
{
// there is atless one element to display
break;
}
}
}
else
{
QModelIndex useIndex
= sourceModel
()->index
(index.
row(),
1, index.
parent());
QString type
= sourceModel
()->data
(useIndex, Qt
::DisplayRole).
toString();
if ( ! type.contains(filterRegExp()))
{
result = false;
}
else
{
result = true;
}
}
return result;
}
#include "songtreeproxyfilter.h"
#include <QtDebug>
SongTreeProxyFilter::SongTreeProxyFilter(QObject *parent):
QSortFilterProxyModel(parent)
{
}
bool SongTreeProxyFilter::filterAcceptsRow(int sourceRow,
const QModelIndex &sourceParent) const
{
QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
return hasToBeDisplayed(index);
}
bool SongTreeProxyFilter::hasToBeDisplayed(const QModelIndex index) const
{
bool result = false;
// How many child this element have
if ( sourceModel()->rowCount(index) > 0 )
{
for( int ii = 0; ii < sourceModel()->rowCount(index); ii++)
{
QModelIndex childIndex = sourceModel()->index(ii,0,index);
if ( ! childIndex.isValid() )
break;
result = hasToBeDisplayed(childIndex);
if (result)
{
// there is atless one element to display
break;
}
}
}
else
{
QModelIndex useIndex = sourceModel()->index(index.row(), 1, index.parent());
QString type = sourceModel()->data(useIndex, Qt::DisplayRole).toString();
if ( ! type.contains(filterRegExp()))
{
result = false;
}
else
{
result = true;
}
}
return result;
}
To copy to clipboard, switch view to plain text mode
I hope it can help.
Bookmarks