将信号添加到继承自 QGraphicsScene 的类
我想向继承自 QGraphicsScene 的类添加信号。
signals:
void update(std::vector< std::vector<int> > board);
当我这样做时,Qt Creator 警告我忘记了 Q_OBJECT
宏。但我在某处读到,由于 QGraphicsScene
不是从 QObject
继承的,所以我不应该将它放在我的类定义中。但信号需要这个宏。
如何向不继承自 QObject
的类添加信号?
boardgui.h
#ifndef BOARDGUI_H
#define BOARDGUI_H
#include <QGraphicsView>
#include <QGraphicsScene>
class BoardGUI : public QGraphicsScene
{
Q_OBJECT
public:
BoardGUI(QGraphicsView*& view, int dimension);
~BoardGUI();
void buildBoard();
signals:
void update(std::vector< std::vector<int> > board);
private:
int dimension;
QGraphicsView* view;
};
#endif // BOARDGUI_H
I want to add a signal to a class that inherits from QGraphicsScene
.
signals:
void update(std::vector< std::vector<int> > board);
When I do this, Qt Creator warns me that I forgot the Q_OBJECT
macro. But somewhere I read that since QGraphicsScene
doesn't inherit from QObject
, I shouldn't put it in my class definition. But signals need this macro.
How do I add a signal to a class that doesn't inherit from QObject
?
boardgui.h
#ifndef BOARDGUI_H
#define BOARDGUI_H
#include <QGraphicsView>
#include <QGraphicsScene>
class BoardGUI : public QGraphicsScene
{
Q_OBJECT
public:
BoardGUI(QGraphicsView*& view, int dimension);
~BoardGUI();
void buildBoard();
signals:
void update(std::vector< std::vector<int> > board);
private:
int dimension;
QGraphicsView* view;
};
#endif // BOARDGUI_H
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
根据要求重新发布为答案:
update() 是你的信号吗?您尝试自己实现该信号吗?如果是,不要这样做,信号是由 moc 定义的。
Reposting as answer, as requested:
Is update() your signal? did you try to implement the signal yourself? If yes, don't do that, signals are defined by moc.
QGraphicsScene确实继承自QObject。您可以查阅文档。
http://doc.qt.io/qt-5/qgraphicsscene.html
QGraphicsScene does inherit from QObject. You can consult the documentation.
http://doc.qt.io/qt-5/qgraphicsscene.html
Vtables 与虚函数有关。当您遇到像
Undefined reference to vtable
这样的错误时,它无法找到虚拟函数的实现(我认为)。你的所有功能都实现了吗?也许您在不应该使用的时候使用了virtual
关键字?或者反之亦然?另外,你说你想添加一个信号,但你的代码块显示一个插槽?你能多展示一下你的班级吗?
Vtables have something to do with virtual functions. When you have an error like
Undefined reference to vtable
then it cannot find an implementation for a virtual function (I think). Have you implemented all of your functions? Maybe you used thevirtual
keyword when you weren't supposed to? Or vice versa?Also, you say that you want to add a signal, but your code chunk shows a slot? Could you show a little more of your class?
如果您之前没有在项目中使用 Q_Object 实现任何类,并且您自己添加了 Q_OBJECT 行,则会出现 vtable 错误。如果将另一个 C++ 类添加到继承自 QObject 的项目中,则可以解决此问题。如果不需要,您可以删除您创建的此类。我不确定为什么会发生这种情况,但这是解决问题的简单方法。也许创建者在 .当您添加继承自 qobject 的类时,请创建 pro 文件。
If you have not implemented any class with Q_Object before to the project and you add the Q_OBJECT line yourself, you get the vtable error. If you add another c++ class to the project that inherits from QObject, then you get rid of this problem. You can remove this class you created if you dont need it. I am not sure why this happens, but this is easy way to get rid of the problem. Maybe the creator adds some line to the . pro file when you add a class that inherits from qobject.
你实现了你的析构函数吗?如果没有,请尝试将
~BoardGUI();
更改为~BoardGUI(){};
Have you implemented your destructor? If not, try changing
~BoardGUI();
to~BoardGUI(){};
您是否已将 boardgui.h 添加到 MOC 预处理器的文件列表中?任何使用 QObject 和信号/槽的类都需要通过元对象编译器,以便它可以生成信号背后的实际代码。
我不确定 Qt Creator 是否自动处理这个问题。
Have you added boardgui.h to the MOC preprocessor's file list? Any class that uses QObject and Signals/Slots needs to be passed through the Meta-Object Compiler so it can generate the actual code behind the signals.
I am unsure if Qt Creator handles this automatically.
您只需自己继承
QObject
即可。boardgui.h
如果您使用Qt Creator,您不必担心moc,一切都应该由Qt Creator处理。
You simply have to inherit from
QObject
yourself.boardgui.h
If you are using Qt Creator, you shouldn't have to worry about moc, everything should be handled by Qt Creator.