Qt setFlags方法不接受int类型

QGraphicsItem item;
item.setFlags(0x01);  //语法不过,提示无法从int转flags

主要是因为QFlags的赋值运算符不允许直接输入整型数,Qt文档的说明

The QFlags class is a template class, where Enum is an enum type. QFlags is used throughout Qt for storing combinations of enum values.
The traditional C++ approach for storing OR-combinations of enum values is to use an int or uint variable. The inconvenience with this approach is that there’s no type checking at all; any enum value can be OR’d with any other enum value and passed on to a function that takes an int or uint.
Qt uses QFlags to provide type safety.
If you try to pass a value from another enum or just a plain integer other than 0, the compiler will report an error. If you need to cast integer values to flags in a untyped fashion, you can use the explicit QFlags constructor as cast operator.

解决办法是构造QFlags

item.setFlags(QGraphicsItem::GraphicsItemFlags(0x01));  

然后,QFlags提供了与、或、非、异或运算符,所以遇到整型数或者非模板(QFlags< Enum>)枚举变量都要先转换为QFlags对象再作运算和赋值。但奇怪的是,只有与运算是支持整型数操作数的,而其余运算只允许操作数是QFlags对象或者模板枚举类型Enum,所以遇到这个差异时不用觉得奇怪:


QFlags operator&(int mask) const  //支持整型操作数
QFlags operator&(uint mask) const //支持整型操作数
QFlags operator&(Enum mask) const
QFlags & operator&=(int mask)  //支持整型操作数
QFlags & operator&=(uint mask) //支持整型操作数
QFlags & operator&=(Enum mask)
QFlags & operator=(const QFlags &other)
QFlags operator^(QFlags other) const
QFlags operator^(Enum other) const
QFlags & operator^=(QFlags other)
QFlags & operator^=(Enum other)
QFlags operator|(QFlags other) const
QFlags operator|(Enum other) const
QFlags & operator|=(QFlags other)
QFlags & operator|=(Enum other)
QFlags operator~() const

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值