Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enums cannot be compared in Qt 5 #89

Open
awilfox opened this issue Jan 6, 2023 · 2 comments · May be fixed by #91
Open

Enums cannot be compared in Qt 5 #89

awilfox opened this issue Jan 6, 2023 · 2 comments · May be fixed by #91

Comments

@awilfox
Copy link

awilfox commented Jan 6, 2023

3b1931e added operator< to MetaEnumVariable. However, contrary to the commit message, no call to QMetaType::registerComparators is made.

In Qt 6, this works because it uses introspection to determine if operator< is implemented in the class. However, in Qt 5 the call must be explicit.

Oddly, it seems to work on most platforms anyway, somehow… except 64-bit PowerPC. On this platform, using Qt 5.15 + KDE Patch Collection, and GCC 8.5.0, with -O2, or -O0 -fstack-protector -fschedule-insns -fmove-loop-invariants -finline-functions-called-once -fguess-branch-probability (minimum optimisation flags needed to trigger):

FAIL!  : TestBuiltinSyntax::testEnums(gadget-enums-compare05) Compared values are not the same                                          
   Actual   (result): "true"                                 
   Expected (output): "false"                                                                                                           
   Loc: [/usr/src/packages/user/grantlee/src/grantlee-5.3.1/templates/tests/testbuiltins.cpp(482)]
PASS   : TestBuiltinSyntax::testEnums(gadget-enums-compare06)                                                                           
FAIL!  : TestBuiltinSyntax::testEnums(gadget-enums-compare07) Compared values are not the same
   Actual   (result): "false"                                                                                                           
   Expected (output): "true"                                                                                                            
   Loc: [/usr/src/packages/user/grantlee/src/grantlee-5.3.1/templates/tests/testbuiltins.cpp(482)]                                      

Adding a call to registerComparators in testbuiltins makes it pass:

diff --git a/templates/tests/testbuiltins.cpp b/templates/tests/testbuiltins.cpp
index fe7e4ed..82a065d 100644
--- a/templates/tests/testbuiltins.cpp
+++ b/templates/tests/testbuiltins.cpp
@@ -298,6 +298,7 @@ void TestBuiltinSyntax::testObjects()
   Q_UNUSED(s3);
 
   QMetaType{qMetaTypeId<MetaEnumVariable>()}.create(nullptr);
+  QMetaType::registerComparators<MetaEnumVariable>();
 }
 
 void TestBuiltinSyntax::testTruthiness_data()
    Start 2: testbuiltins
1/1 Test #2: testbuiltins .....................   Passed    0.25 sec

100% tests passed, 0 tests failed out of 1
pinotree added a commit to pinotree/grantlee that referenced this issue Aug 13, 2023
This is needed to ensure that MetaEnumVariable can be properly handled
for comparisons, e.g. as done via QVariant::compare().

Followup of commit 3b1931e

Fixes steveire#89
@pinotree
Copy link
Contributor

Changing the test only fixes the test itself, and not comparisons done when using grantlee in some other application/library.

I submitted #91 for this.

@pinotree pinotree linked a pull request Aug 13, 2023 that will close this issue
@awilfox
Copy link
Author

awilfox commented Aug 13, 2023

Thanks. I figured this would need to be called somewhere, but wasn't sure the best place for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants