QObject::No such slot 接收者::槽函数名(参数)或QObject::connect: No such signal 发送者::信号名(参数)

Qt运行时报错:QObject::connect: No such signal以及QMetaObject::connectSlotsByName:No matching signal for

  • Chapter1 QObject::No such slot 接收者::槽函数名(参数)或QObject::connect: No such signal 发送者::信号名(参数)
  • Chapter2 Qt官方解释

Chapter1 QObject::No such slot 接收者::槽函数名(参数)或QObject::connect: No such signal 发送者::信号名(参数)

在Qt中使用带参数的自定义槽函数进行父子窗口通信时,遇到了如下错误提示:

QObject::connect: No such signal Form::sendListGot(const QString &list) in ***

只是因为在编写自定义信号和自定义槽函数时,虽然信号和槽都是带参数的,但是使用connect进行连接时,只能在函数中填写参数的数据类型而不能将形参名列出。

信号与槽函数在连接时是无参数的,只写参数类型,如下:

正确格式:connect(form,SIGNAL(sendListGot(const QString)),this,SLOT(getListGot(const QString)));

错误格式:connect(form,SIGNAL(sendListGot(const QString str)),this,SLOT(getListGot(const QString str)));

Chapter2 Qt官方解释

文章来源:https://doc.qt.io/qt-5/signalsandslots.html

The other way to connect a signal to a slot is to use QObject::connect() and the SIGNAL and SLOT macros. The rule about whether to include arguments or not in the SIGNAL() and SLOT() macros, if the arguments have default values, is that the signature passed to the SIGNAL() macro must not have fewer arguments than the signature passed to the SLOT() macro.

All of these would work:

connect(sender, SIGNAL(destroyed(QObject*)), this, SLOT(objectDestroyed(Qbject*)));
connect(sender, SIGNAL(destroyed(QObject*)), this, SLOT(objectDestroyed()));
connect(sender, SIGNAL(destroyed()), this, SLOT(objectDestroyed()));

But this one won’t work:

connect(sender, SIGNAL(destroyed()), this, SLOT(objectDestroyed(QObject*)));

…because the slot will be expecting a QObject that the signal will not send. This connection will report a runtime error.

Note that signal and slot arguments are not checked by the compiler when using this QObject::connect() overload.

你可能感兴趣的:(Qt经验总结,工控软件,qt,c++)