QT中添加Q_OBJECT出现的问题

9 篇文章 0 订阅
4 篇文章 0 订阅

Multiple Inheritance Requires QObject to Be First(多重继承QObject一定要放在前面)

我在用class My_Node : public QGraphicsItem,public QObject来生成自己的类时,想使用信号和槽机制,但这是dys_node.h中没有Q_OBJECT,不能使用信号槽。我将Q_OBJECT加上之后,出现几个不知道是什么的错误。然后我将工程目录下的Makefile删除,然后重新编译、链接文件,刚才的错误消失了,但出现如下三个错误:

error: 'staticMetaObject' is not a member of 'QGraphicsItem'

error: 'qt_metacast' is not a member of 'QGraphicsItem'

error: 'qt_metacall' is not a member of 'QGraphicsItem'

通过google,我发现这个错误是由于没有继承QObject类而引起的,但是我已经继承了QObject类了啊,到底是怎么回事呢?

最后终于在网上找到了答案,当多继承的时候,要将QObject放在前面,即将类的声明改为

class My_Node : public QObject,public QGraphicsItem

问题就解决了!!!

下面是我从网络上找到的说明原文:

Just got this error message while compiling a tiny class that derives from QTreeWidgetItem:

error C2039: 'staticMetaObject' : is not a member of 'QTreeWidgetItem'

What this is saying is that QTreeWidgetItem does not inherit from QObject, meaning that your own, singly-inherited class also does not inherit from QObject. Inheriting from QObject is one of the prerequisites to using the Q_OBJECT macro, which, if you’re anything like me, you automatically insert into any Qt GUI related class.

If you’re not using any of the meta object stuff in your subclass, such as signals/slots or properties, just take out the Q_OBJECT macro. If you need to use signals and slots, you’ll need to make your subclass multiply-inherit from QObject as well. If you take this route, remember that “Multiple Inheritance Requires QObject to Be First”, otherwise you’ll get either the same error as above, or something along the lines of “YourClass inherits from two QObject subclasses” from the moc。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值