为什么不会用Q_OBJECT宏编译(链接)到位? [英] Why won't this compile (link) with the Q_OBJECT macro in place?

查看:365
本文介绍了为什么不会用Q_OBJECT宏编译(链接)到位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PyQt制作了一个项目的原型,并使它在那里工作,现在我试图将其转换为C ++,并有一些问题。

I made a prototype of a project with PyQt and made it work there, now I'm trying to convert it to C++ and am having some problems.

我不把Q_OBJECT宏,它编译和工作,但如果我注释掉,我得到以下错误:

If I don't put the Q_OBJECT macro in, it compiles and works, but if I comment it out, I get the following errors:

Undefined symbols:
  "vtable for MapView", referenced from:
      MapView::~MapView()in mapview.o
      MapView::~MapView()in mapview.o
      MapView::MapView(QObject*)in mapview.o
      MapView::MapView()in mapview.o
  "MapView::staticMetaObject", referenced from:
      MapView::MapView(QObject*)in mapview.o
      MapView::MapView()in mapview.o

这是标题:

#ifndef MAPVIEW_H
#define MAPVIEW_H

#include <QtGui>
#include <QObject>

class MapView : public QGraphicsScene
{
    //Q_OBJECT

public:
    MapView();
    explicit MapView(QObject *parent = 0);
    QGraphicsPixmapItem *mappixmap;
    ~MapView();

private:
    bool dragging;
    float offsetX, offsetY, downoffsetX, downoffsetY;

protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event);
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);

};

#endif // MAPVIEW_H

我只是在这里省略Q_OBJECT宏?

Secondary question is will Bad Things happen if I just omit the Q_OBJECT macro here?

是的,我知道这是愚蠢的调用QGraphicsScene一个视图。

And yes, I'm aware of that it's stupid to call a QGraphicsScene a "view".

推荐答案

这种错误通常发生在你添加 Q_OBJECT 宏并忘记重新运行 moc 。如果使用 qmake ,只需在添加宏后运行 make qmake

This kind of errors usually happen when you add the Q_OBJECT macro and forget to rerun moc. If you use qmake, just run make qmake after you added the macro.

对于你的第二个问题:在没有 Q_OBJECT 宏的情况下,你将不能使用信号/槽(以及其他)。有关此问题的详情,请参见文档

As for your second question: you won't be able to use signals/slots (among other things) without the Q_OBJECT macro. See the docs for more information about this.

这篇关于为什么不会用Q_OBJECT宏编译(链接)到位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆