没有匹配的连接调用 - 当我设置 7 个参数时。使用 6 个参数可以正常工作

No matching call to connect - when I set 7 parameters. works fine with 6 parameters

信号的参数个数有限制吗?

/home/.../testerslow/jjjj.h:36: error: no matching function for call to 'Controller::connect(Controller* const, void (Controller::*)(int, int, int, int, int, int, int), Worker*&, void (Worker::*)(int, int, int, int, int, int, int))'
         connect (this, &Controller::operate, worker, &Worker::doWork);

在下面的代码中,只需将信号槽dowork减少1个参数并运行,错误就会消失。

第7个参数的传递方式是什么?

可重现的例子:

#ifndef JJJJ
#define JJJJ
#include <QQuickItem>
#include <QDebug>
#include <QThread>


class Worker : public QObject
{
    Q_OBJECT

public:
    Worker() { }

public slots:
    void doWork (int one, int b, int c, int d, int e, int f, int h)
    {
        emit resultReady("");
    }

signals:
    void resultReady(const QString &result);
};

class Controller : public QObject
{
    Q_OBJECT

    QThread workerThread;

public:
    Controller() {
        Worker *worker = new Worker;

        connect (&workerThread, &QThread::finished, worker, &QObject::deleteLater);
        connect (this, &Controller::operate, worker, &Worker::doWork);
        connect (worker, &Worker::resultReady, this, &Controller::handleResults);
        worker->moveToThread(&workerThread);

        workerThread.start();
    }

    ~Controller() {
        workerThread.quit();
        workerThread.wait();
    }

public slots:
    void handleResults(const QString &) {}

signals:
    void operate(int, int, int,int,int,int, int);
};
#endif // JJJJ

根据 documentation, you are limited to calls with six arguments or less using new-style syntax unless your compiler supports variadic template signatures. A quick solution is to create a container type and register it with QMetaType using Q_DECLARE_METATYPE and qRegisterMetaType.

首先声明数据类型:

//foo.h
#include <QMetaType>

struct Foo 
{
    int one, b, c, d, e, f, h;
}
Q_DECLARE_METATYPE(Foo)

由于您使用的是线程,这意味着排队的信号,您还需要在运行时使用 qRegisterMetaType 注册元类型,通常在 main 或一些只调用一次的初始化函数中:

//main.cpp
#include <QApplication>
#include <QMetaType>
#include "foo.h"

int main(...)
{
    QApplication app(argc, argv);
    qRegisterMetaType<Foo>();
    ... //do more things here
    return app.exec()
}

您现在应该能够在信号和槽连接中使用 Foo 而不会出现问题,使用以下信号和槽签名:

void doWork(Foo foo);

void operate(Foo);