1

I just recently added a QLabel to a ui form in a Qt project, set its text alignment to a custom value, and was then surprised to find that the auto-generated code for the ui class caused a compiler error in MSVC:

ui_projectwidget.h:109:
error: C2664: 'void QLabel::setAlignment(Qt::Alignment)' : cannot convert argument 1 from 'int' to 'Qt::Alignment'

Qt::Alignment is a flag to indicate left/right/center/etc. text justification. The offending line in ui_projectwidget.h is:

componentName->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop);

In this project, I overload operator | for a scoped enum, MIBC::ItemType, that is unrelated to Qt components:

using FlagType = int64_t;
enum class ItemType: FlagType {
      type1, type2, etc
}

using Ty = std::underlying_type_t<MIBC::ItemType>;

inline MIBC::ItemType operator| (MIBC::ItemType lhs, MIBC::ItemType rhs) {
    return static_cast<MIBC::ItemType>(static_cast<Ty>(lhs) | static_cast<Ty>(rhs));
}

I was able to fix the compiler error by adding another overload for operator | for Qt::AlignmentFlag:

inline Qt::AlignmentFlag operator|(Qt::AlignmentFlag lhs, Qt::AlignmentFlag rhs) {
    return static_cast<Qt::AlignmentFlag>(static_cast<int>(lhs) | static_cast<int>(rhs));
}

Even though I've "fixed" the problem, I still don't understand it. What broke the default operator | such that it no longer accepts Qt::AlignmentFlag? Should I restructure my scoped enum in some way that it doesn't interfere with other bitwise-or operators?

Carlton
  • 4,217
  • 2
  • 24
  • 40
  • 1
    "The QFlags class provides a type-safe way of storing OR-combinations of enum values. More..." source: http://doc.qt.io/qt-4.8/qflags.html Including an example and fix that looks very similar to your problem. – Richard Critten Oct 07 '16 at 14:12
  • 1
    Without a complete program, it's hard to tell. But it looks like you're in a scope where your definition is hiding Qt's `operator|` (which is in the global namespace). Note also that Qt gives you some macros to help you define your own flags combinator more easily if you want. – Toby Speight Oct 07 '16 at 14:15
  • @RichardCritten I wish I had discovered `QFlags` about 20,000 lines of code sooner... – Carlton Oct 07 '16 at 14:25
  • @TobySpeight That was the problem indeed. Adding `using ::operator|;` fixed the problem. – Carlton Oct 07 '16 at 14:26
  • 1
    You haven't fixed the problem, you've plastered over it. You must create a minimal test case that reproduces the issue, and fix your question to include it. Everything must fit into a single `main.cpp` that you should include verbatim, and you must remove everything that doesn't demonstrate the issue. – Kuba hasn't forgotten Monica Oct 07 '16 at 15:53

1 Answers1

1

Most likely, you declared the operator in the global namespace, breaking the argument dependent lookup. Instead, you should have put it in the namespace of its arguments, and let ADL pick it up.

Community
  • 1
  • 1
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • Yes, moving my custom overloads to the global namespace works as well. I don't think my original question is salvageable at this point; my problem really had nothing to do with Qt or scoped enums specifically. I can't seem to find a SO question that specifically addresses this problem, so I may just repost a new question. – Carlton Oct 07 '16 at 16:19