QT信号和槽意外错误

4

这让我疯了...之前它还能用,但现在不行了。我定义了Q_SLOTS和Q_SIGNALS并尝试连接它们。它在一定程度上是有效的...然后突然所有东西都停止工作了,现在我得到了错误提示。我的代码如下:

ControllerLogic.h

#ifndef CONTROLLERLOGIC_H
#define CONTROLLERLOGIC_H

#include "initdataaccess.h"
#include "mainframe.h"
#include <QtGui>
#include "initializationdatastructure.h"


/** This is a controller class; refering to the model-view-controller
 *  architecture.
 */

class ControllerLogic : public QObject
{
    Q_OBJECT
public:
    ControllerLogic(InitDataAccess *initDataAccess, MainFrame *mainFrame);

Q_SIGNALS:
    void Signal();

private:
    void setMainFrame(MainFrame mainFrame);

public Q_SLOTS:
    void receive();

};

#endif // CONTROLLERLOGIC_H

ControllerLogic.cpp

#include "controllerlogic.h"
#include "licensedataaccess.h"
#include <qobjectdefs.h>

// obsolete...may be used later

ControllerLogic::ControllerLogic(InitDataAccess *initDataAccess, MainFrame *mainFrame)
{
    connect(this, SIGNAL(signal()), mainFrame, SLOT(PrintTestSlot()));
}

void ControllerLogic::receive(){
    qDebug()<<"RECEIVE";
}

void ControllerLogic::Signal(){
    qDebug()<<"SIGNAL";
}

错误

moc_controllerlogic.obj:-1: error: LNK2005: "protected: void __thiscall ControllerLogic::Signal(void)" (?Signal@ControllerLogic@@IAEXXZ) already defined in controllerlogic.obj

release\TSLSuite.exe:-1: error: LNK1169: one or more multiply defined symbols found

我也尝试将该信号定义为以下内容:

public:
Q_SIGNAL void Signal();

但是我得到了相同的错误。

究竟发生了什么? 请帮助我!

谢谢!

1个回答

9
问题在于您试图定义一个名为Signal()的函数。
Qt会自动生成“signal”函数的主体,如果您尝试创建自己的定义,则会出现您所描述的错误。
(顺便说一下,您的连接语句似乎有问题,请尝试:s/signal/Signal/

好的,那么如果Qt为我生成函数,我该如何定义我想在信号中执行的操作呢? - PTBG
一个信号就是一个信号,没有别的。如果你想对这个信号做出反应,那么你可以通过信号/槽连接来连接它,并在你的槽中执行自己的代码。 - Chris
哦,我现在明白了,非常感谢。我对信号/槽机制有了错误的理解。 - PTBG
非常感谢您的回答。花了太多时间来尝试修复这个错误。 - Norbert Szenasi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接