This problem is solved, I have put code for reference.
Thanks to everyone, this forum rocks...
{
for( int r=0; r<5; r++ )
{
for( int i=0; i<3; i++ )
{
child->setEditable( false );
item->appendRow( child );
}
model->setItem(r, 0, item);
}
model
->setHorizontalHeaderItem
( 0,
new QStandardItem( "Column 1" ) );
return model;
}
QAbstractItemModel *FilterDialog::createModel(QObject *parent)
{
QStandardItemModel *model = new QStandardItemModel(5, 1, parent);
for( int r=0; r<5; r++ )
{
QStandardItem *item = new QStandardItem( QString("Row:%0").arg(r));
for( int i=0; i<3; i++ )
{
QStandardItem *child = new QStandardItem( QString("Item %0").arg(i) );
child->setEditable( false );
item->appendRow( child );
}
model->setItem(r, 0, item);
}
model->setHorizontalHeaderItem( 0, new QStandardItem( "Column 1" ) );
return model;
}
To copy to clipboard, switch view to plain text mode
FilterDialog
::FilterDialog(QWidget *parent
){
setupUi(this);
proxyModel = new SongTreeProxyFilter;
proxyModel->setDynamicSortFilter(true);
treeView->setModel(proxyModel);
proxyModel->setSourceModel(itemModel);
connect(FilterLineEdit,
SIGNAL(textChanged
(QString)),
this, SLOT(filterChanged()));
}
void FilterDialog::filterChanged()
{
Qt::CaseSensitivity caseSensitivity = Qt::CaseInsensitive;
QString strPattern
= "^" + FilterLineEdit
->text
();
QRegExp regExp
(strPattern, caseSensitivity
);
proxyModel->setFilterRegExp(regExp);
}
FilterDialog::FilterDialog(QWidget *parent)
: QDialog(parent)
{
setupUi(this);
proxyModel = new SongTreeProxyFilter;
proxyModel->setDynamicSortFilter(true);
treeView->setModel(proxyModel);
QAbstractItemModel * itemModel = createModel(this);
proxyModel->setSourceModel(itemModel);
connect(FilterLineEdit, SIGNAL(textChanged(QString)),
this, SLOT(filterChanged()));
}
void FilterDialog::filterChanged()
{
QRegExp::PatternSyntax syntax = QRegExp::RegExp;
Qt::CaseSensitivity caseSensitivity = Qt::CaseInsensitive;
QString strPattern = "^" + FilterLineEdit->text();
QRegExp regExp(strPattern, caseSensitivity);
proxyModel->setFilterRegExp(regExp);
}
To copy to clipboard, switch view to plain text mode
//SubClass of QSortFilterProxyModel
bool TreeProxyFilter::filterAcceptsRow(int sourceRow,
{
QModelIndex index
= sourceModel
()->index
(sourceRow,
0, sourceParent
);
return ShowThis(index);
}
bool TreeProxyFilter
::ShowThis(const QModelIndex index
) const {
bool retVal = false;
//Gives you the info for number of childs with a parent
if ( sourceModel()->rowCount(index) > 0 )
{
for( int nChild = 0; nChild < sourceModel()->rowCount(index); nChild++)
{
QModelIndex childIndex
= sourceModel
()->index
(nChild,
0,index
);
if ( ! childIndex.isValid() )
break;
retVal = ShowThis(childIndex);
if (retVal)
{
break;
}
}
}
else
{
QModelIndex useIndex
= sourceModel
()->index
(index.
row(),
0, index.
parent());
QString type
= sourceModel
()->data
(useIndex, Qt
::DisplayRole).
toString();
if ( ! type.contains(filterRegExp()))
{
retVal = false;
}
else
{
retVal = true;
}
}
return retVal;
}
//SubClass of QSortFilterProxyModel
bool TreeProxyFilter::filterAcceptsRow(int sourceRow,
const QModelIndex &sourceParent) const
{
QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
return ShowThis(index);
}
bool TreeProxyFilter::ShowThis(const QModelIndex index) const
{
bool retVal = false;
//Gives you the info for number of childs with a parent
if ( sourceModel()->rowCount(index) > 0 )
{
for( int nChild = 0; nChild < sourceModel()->rowCount(index); nChild++)
{
QModelIndex childIndex = sourceModel()->index(nChild,0,index);
if ( ! childIndex.isValid() )
break;
retVal = ShowThis(childIndex);
if (retVal)
{
break;
}
}
}
else
{
QModelIndex useIndex = sourceModel()->index(index.row(), 0, index.parent());
QString type = sourceModel()->data(useIndex, Qt::DisplayRole).toString();
if ( ! type.contains(filterRegExp()))
{
retVal = false;
}
else
{
retVal = true;
}
}
return retVal;
}
To copy to clipboard, switch view to plain text mode
Bookmarks