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?