Filter a column with QSortFilterProxyModel
-
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?
(google translate) -
@JBosco
I see your problem because of https://doc.qt.io/qt-5/qsortfilterproxymodel.html#filterRegularExpression-propIf no QRegularExpression or an empty string is set, everything in the source model will be accepted.
(BTW, use
setFilterRegExpression
now instead ofsetFilterRegExp
, I think despite the docs).You could try:
setFilterRegExpression("^$")
setFilterFixedString("")
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.
P.S.
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
OK, good, I didn't knowsetFilterRegExpression()
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. -
@JonB Qt doesn't modify your regular expression unless you use a function that explicitly does that like QRegularExpression::anchoredPattern.
-
@SGaist
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.EDIT Ah, https://doc.qt.io/qt-5/qregexpvalidator.html#details
When
QRegExpValidator
determines whether a string isAcceptable
or 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.