[Solved] QRegExp and QRegularExpression
-
I was just about to do some fancy stuff with a "QSortFilterProxyModel":http://qt-project.org/doc/qt-5.1/qtcore/qsortfilterproxymodel.html, using one of its filter functions ("1":http://qt-project.org/doc/qt-5.1/qtcore/qsortfilterproxymodel.html#match , "2":http://qt-project.org/doc/qt-5.1/qtcore/qsortfilterproxymodel.html#filterRegExp-prop ).
After reading "this blog post":http://dangelog.wordpress.com/2012/04/07/qregularexpression/ I thought that it would be a good time to switch from the "old QRegExp":http://qt-project.org/doc/qt-5.1/qtcore/qregexp.html to the "new QRegularExpression":http://qt-project.org/doc/qt-5.1/qtcore/qregularexpression.html .
And now it seems that the proxy model has never heard of all the new features, bugfixes and speed improvements that QRegularExpression supposedly brings along, it refuses to make use of it. Being that Qt5 has been around for about a year, I wonder if I am just missing something here? Did I overlook a function? Is QRegularExpression not yet supported by one of the classes that could get the most out of it (a proxy model is for sorting and filtering, right?)?
This might not exactly be an "I got a problem, may somebody help me?"-thread, but since for the past months I started to struggle with Qt where I never had to before, I am a little bit confused and in need of some clarifying words. If somebody knows a solution to my current problem, that would be appreciated, too. :)
Until then, I'll take a look into the old filter function of QSortFilterProxyModel and hope that I can make it compatible with the new regexps for my own purposes. Have a nice day.
-
Hi,
Indeed it seems that the class has not yet been ported to make use of it. In between, you could implement filterAcceptsRow using QRegularExpression.
Hope it helps
-
I stumbled over that one, but I don't really know what to do with it. I obviously still can't use setFilterRegExp, so how do I proceed?
EDIT: I guess there is an example using QDates, maybe I'll take a look into that tomorrow.
-
Just add your own setFilterRegExp that uses QRegularExpression, store it as member variable and reimplement filterAcceptsRow to use it. You should be good to filter then
-
I guess I'll go with setFilterRegularExpression then, since it seems fitting. :P
As I said: I'll try that tomorrow, thanks for the hints. :) I'll come back here if major accidents happen and/or people get injured during reimplementation. -
I tried it out, and it seems to work. :) Thank you. But I still hope that Qt5 will receive some polish in the future!
Here my code, for reference:
@void CustomSortFilterProxyModel::setFilterRegular[removed]const QRegularExpression ®exp)
{
filter = regexp;
this->invalidateFilter();
}@EDIT: Did I activate the swear-filter in that code-block? :D I just called the function what I said I would, in my previous post. :-?
@bool CustomSortFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
if(filter.pattern().isEmpty())
{
return true;
}
if(this->filterKeyColumn() == -1)
{
for(int column = 0 ; column < this->sourceModel()->columnCount(source_parent) ; ++column)
{
QModelIndex sourceIndex = this->sourceModel()->index(source_row, column, source_parent);
QString key = this->sourceModel()->data(sourceIndex, this->filterRole()).toString();
if(key.contains(filter))
{
return true;
}
}
return false;
}
QModelIndex sourceIndex = this->sourceModel()->index(source_row, this->filterKeyColumn(), source_parent);
if(!sourceIndex.isValid())//column may not exist
{
return true;
}
QString key = this->sourceModel()->data(sourceIndex, this->filterRole()).toString();
return(key.contains(filter));
}@
I basically just had to copy it from the Qt sources, replace all the calls of private members, and things were done. Since QString is already updated to use the new QRegularExpression, the changes to the Qt sources themselves would be minimal.One more thanks. :)