Qt Connect "No such slot" when Slot Definitely Does Exist

Qt Connect “No such slot” when Slot Definitely Does Exist

In this article, we will discuss a common issue in Qt programming, specifically with the connect function and its error message "No such slot". This issue can arise even if the slot exists and is properly declared.

The Problem

When trying to connect signals and slots using the connect function, you may encounter an error message stating that there is no such slot. Despite this message, the slot may indeed exist and be properly declared in your code.

Example 1: QMainWindow and QUuid Signals

For instance, consider a MainWindow class with a signal and a slot both involving QUuid. The error message will still occur even if the slot exists:

class MainWindow : public QMainWindow {
 Q_OBJECT

public:
 MainWindow() {
 QPushButton* button = new QPushButton("Test", this);
 QObject::connect(button, &QPushButton::clicked, this, SLOT(handleButtonClick(QVariant)));
 }

public slots:
 void handleButtonClick(const QVariant& value) {}

};

Example 2: Custom QObject Class

In another example, a custom LocationMonitor class inherits from QObject. It has signals and slots declared, but the connect function still raises an error message:

class LocationMonitor : public QObject {
 Q_OBJECT

public:
 LocationMonitor() {
 QGeoAreaMonitor* monitor = QGeoAreaMonitor::createDefaultMonitor(this);
 QObject::connect(monitor, SIGNAL(areaEntered(QGeoPositionInfo)), this, SLOT(areaEnteredd(QGeoPositionInfo)));
 }

public slots:
 void areaEnteredd(QGeoPositionInfo info) {}

};

Solution

To resolve this issue, make sure that the slot is properly declared using the Q_SLOTS macro or the SIGNAL and SLOT macros. Additionally, ensure that the signal and slot have matching parameter types.

In the case of Example 1, simply changing the handleButtonClick slot declaration to:

void handleButtonClick();

without parameters should resolve the issue. For Example 2, make sure that the areaEnteredd slot is declared with the correct parameter type, such as QGeoPositionInfo.

Additional Tips

When dealing with signals and slots in Qt, keep the following tips in mind:

  • Make sure to use the correct syntax for connecting signals and slots. For example, using SIGNAL and SLOT macros or C++ syntax.
  • Verify that your signal and slot declarations match each other's parameter types.
  • If you're using custom classes, ensure they inherit from QObject correctly.

By following these tips and guidelines, you should be able to resolve the "No such slot" error message and successfully connect signals and slots in your Qt applications.