[Qt-interest] QAbstractItemModel::match and MatchFlags

Andre Somers andre at familiesomers.nl
Sat Jun 18 08:16:06 CEST 2011


Op 18-6-2011 0:51, Eric Clark schreef:
>
> Hello Everyone,
>
> I have been looking at this for quite some time trying to figure why 
> it is the way it is because the Qt guys are usually pretty darn good 
> and usually have a very good reason for why they do things the way 
> they do, but I cannot figure this one out. I am making a custom match 
> function in my model and I am trying to handle all of the flags that 
> can be passed into the function, but there are a few flags that are 
> really throwing me off:
>
> MatchFlag::MatchContains = 1
>
> MatchFlag::MatchStartsWith = 2
>
> MatchFlag::MatchEndsWIth = 3
>
> The flag for MatchEndsWith has two bits flipped, and the two bits 
> (position 0 and 1) match the bits for MatchContains and 
> MatchStartsWith. In other words:
>
> MatchEndsWith == MatchContains | MatchStartsWith;
>
> This seems very wrong to me. To me, it seems like it should really be:
>
> MatchFlag::MatchContains = 3
>
> MatchFlag::MatchStartsWith = 2
>
> MatchFlag::MatchEndsWIth = 1
>
> MatchContains == MatchEndsWith | MatchStartsWith;
>
> I do not see how setting the ends with flag should also be setting the 
> starts with and contains flag. I hope I explained this right. Please 
> can someone explain to me what I am seeing? Is this my imagination or 
> this an actual error in the code?
>
>
Good question!
If I look at the Qt::MatchFlags documentation, it seems like the flags 
are a combination of a traditional enum with some flags functionality 
added on top. I disagree that MatchContains == MatchEndsWith | 
MatchStartsWith. That just isn't true. It seems to me, that you need to 
use it like this:

if ((matchValue && 0xF) == myRequiredMatchType) { ...

and test for the modifier flags (with values 16 and up) using the normal
matchValue.testFlag(Qt::MatchRecursive);

I must say: it is not very pretty, because even though it is a Flags, 
you can not use testFlag() on the lower end of the values.

André

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110618/e9110d3c/attachment.html 


More information about the Qt-interest-old mailing list