Project

General

Profile

can't get Wt::WStringListModel->setFilterRegExp() to work

Added by Mark Travis 5 months ago

I've tried the following:

        std::shared_ptr<Wt::WSortFilterProxyModel>                            filterColumns;
        myRegEx = std::make_unique<std::regex>(columnFilterEntry_->text().toUTF8());
        filterColumns->invalidate();
        filterColumns->setFilterRegExp(std::move(myRegEx));

In the prior example, I also tried to concatenate "\" to the beginning and "\g" to the end as well as "*" in myRegEx.

I've also tried using the output from WRegExpValidator:

    validator = std::make_shared<Wt::WRegExpValidator>("[a-zA-Z0-9._*%+-]{1,30}");
    filterColumns->invalidate();
    filterColumns->setFilterRegExp(validator->regExp());

Nothing seems to work. I've got 830 strings in the WStringListModel with ->setFilterKeyColumn(0). (set to zero) and setFilterRole(Wt::ItemDataRole::Display). I see all 830 rows when the page comes up, but when I type in a couple of characters that I KNOW match at least 20 of the strings, nothing is returned.


Replies (3)

RE: can't get Wt::WStringListModel->setFilterRegExp() to work - Added by Matthias Van Ceulebroeck 5 months ago

Hi Mark,

keep in mind that the regex required here is a std::regex. I created a simple example that does correctly filter some values, which happens correctly.

example.cpp

If I input a regex like ^t.* it filters correctly and shows this and test.

RE: can't get Wt::WStringListModel->setFilterRegExp() to work - Added by Mark Travis 5 months ago

Thanks!!

I changed

auto myRegEx = std::make_unique<std::regex>(columnFilterEntry_->text().toUTF8());

to

auto myRegEx = std::make_unique<std::regex>("^" + columnFilterEntry_->text().toUTF8() + ".*");

And it works!! So simple, but so obscure. I wasn't familiar with regex before now.

RE: can't get Wt::WStringListModel->setFilterRegExp() to work - Added by Matthias Van Ceulebroeck 5 months ago

Ah yes, regex will always try to fully match. E.g. t won't match for test, only for t.
Only test itself and things like t.*, t.+, \w, t[est]{3} will match. This is obviously a limited set of potential match generators, but always shows that a full match is necessary.

    (1-3/3)