为什么添加函数参数会导致无法识别 SLOT()?

Why does adding a function argument cause a SLOT() to be unrecognised?

我有一个class如下:

handler.h:

#ifndef HANDLER_H
#define HANDLER_H

#include <QObject>

class handler : public QObject
{
    Q_OBJECT

public:
    explicit handler(QObject *parent = nullptr);
    ~handler();

public slots:
    void returnHandler(int input);
};

#endif // HANDLER_H

handler.cpp:

#include "handler.h"
#include "otherclass.h"

handler::handler(QObject *parent) : QObject(parent)
{

}

handler::~handler()
{

}

void handler::returnHandler(int input)
{
    otherclass *otherclassPointer = otherclass::getInstance();
    otherclassPointer->returnFunction(input);
}

如图所示,这是一个非常简单的class,目的是接收一个输入并将输入传递给外部class('otherclass')中的一个函数。在我的主应用程序('main.cpp')中,我创建了一个QThread,并在QThread启动时调用returnHandler槽,如下:

main.cpp:

QThread* newThread = new QThread();
handler* handlerPointer = new handler();
handlerPointer->moveToThread(newThread);
connect(newThread, SIGNAL(started()), handlerPointer, SLOT(returnHandler(someInput)));
newThread->start();

我遇到的问题是:

为什么添加参数会导致插槽不再被识别?

编辑:根据下面的一些非常有用和赞赏的 comments/answers,我修改了如下方法:

  1. 在处理程序 class 中创建一个信号,它与 returnHandler 槽的参数相匹配。例如。 void handlerSignal(int).
  2. connect() 中使用了 handlerSignal() SIGNAL 而不是 QThread::started() 信号。
  3. QThread 启动后发出 handlerSignal()

`

QThread* newThread = new QThread();
handler* handlerPointer = new handler();
handlerPointer->moveToThread(newThread);
connect(handlerPointer, SIGNAL(handlerSignal(int)), handlerPointer, SLOT(returnHandler(int)));
newThread->start();
emit handlerPointer->handlerSignal(someInput);

谢谢!

connectstrings 作为要连接的信号槽的标识。宏 SIGNALSLOT 将它们的参数字符串化(为此使用预处理器功能)。因此,SIGNALSLOT 的参数必须是函数名称,参数 types 在括号中。你不能对它们进行参数绑定。

如果您需要连接到零信号,则需要一个零插槽。

两件事:

  1. Qt 期望信号和槽具有相同的参数类型。
  2. 在 SLOT() 中,您必须提供类型,而不是参数名称。
    SLOT(returnHandler(int))
    而不是
    SLOT(returnHandler(someInput))
    Qt 使用信号和槽的名称和参数列表来识别它们。我是你的情况,Qt 寻找一个名为 'returnHandler' 的槽并且只有一个参数,来自类型 'someInput'.