Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Filter a column with QSortFilterProxyModel
JBosco last edited by JBosco
Sorry because I don't speak English very well
To speed up my project, I decided to postpone my problem here. I use the setFilterRegExp function a method of the QSortFilterProxyModel object to filter a column. The concern is that I want to filter the empty cell with it. Can someone give me an idea to do it?
I see your problem because of https://doc.qt.io/qt-5/qsortfilterproxymodel.html#filterRegularExpression-prop
If no QRegularExpression or an empty string is set, everything in the source model will be accepted.
setFilterRegExpressionnow instead of
setFilterRegExp, I think despite the docs).
You could try:
but I don't know if either of those two will work.
Otherwise, I think you will need to override https://doc.qt.io/qt-5/qsortfilterproxymodel.html#filterAcceptsRow and code there for your empty (or NULL) value. That provides an alternative way to filter instead of using the regular expressions/fixed strings.
Do you want the filter to accept/include only empty column, as I have assumed, or do you want it to reject/exclude empty column? If you want to see only non-empty,
setFilterRegExpression(".+")should do it.
JBosco last edited by
Thank you, setFilterRegExpression("^$") works for me
OK, good, I didn't know
setFilterRegExpression()expects a "whole" regular expression, I thought it already implies it had its own
^...$around whatever you specify. Qt docs are not always clear on this in the various places regular expressions are used.
That was not what I meant. I was thinking of somewhere, maybe widget validators, where it was not clear to me whether the pattern you were giving was treated as if it had to match the whole input, i.e. as though it were enclosed in
^$. And in that case specifying
^$would be two literal characters, and not match empty.
QRegExpValidatordetermines whether a string is
Acceptableor not, the regexp is treated as if it begins with the start of string assertion (
^) and ends with the end of string assertion (
$); the match is against the entire input string, or from the given position if a start position greater than zero is given.