0

The following code compiles, but I feel that it should not:

// main.cpp

#include <QObject>
#include <iostream>

class MyClass : public QObject {
    Q_OBJECT;
    
public:
    MyClass();
    void do_it();
        
signals:
    void some_signal(const QString& value);
};

MyClass::MyClass()
{
}

#include "main.moc"

void
MyClass::do_it()
{
    emit some_signal("hello, world!");
}

void foo()
{
    std::cout << "foo called" << std::endl;
}

int
main()
{
    MyClass o;
    o.connect(&o, &MyClass::some_signal, &foo);
    o.do_it();           
    return 0;
}

Specifically speaking, the signature of the function I'm connecting to the MyClass::some_signal() signal does not match the signature of the MyClass::some_signal() signal. So it shouldn't even compile. Am I wrong on that, or am I missing to do something here?

Full disclosure: my actual problem is that a slot I'm connecting to a signal I am emitting does not get called. But I feel this problem might be related, and I need to get this issue sorted out anyway.

  • Perhaps Qt looses the function signature and simply store the pointer without any type information? – Some programmer dude May 03 '23 at 11:52
  • But `foo()` isn't a slot, right? – vahancho May 03 '23 at 11:54
  • @vahancho When using the 'new' signal/slot syntax the callable specified as the slot doesn't need to be declared as such (i.e. no need to use the `slots:` syntax in the class definition) -- any function with a suitable signature will do. – G.M. May 03 '23 at 12:00
  • @G.M. yes, exactly. which begs the question, why does the code compile? foo() doesn't match some_signal(). The compiler is supposed to detect these things. Forgot to mention ... using Qt 6.5.0 and g++ 10.2.1 on Debian Linux 11. – Arcadio Alivio Sincero May 03 '23 at 12:03
  • 1
    From the [documentation](https://doc.qt.io/qt-6/signalsandslots.html): `"In fact a slot may have a shorter signature than the signal it receives because it can ignore extra arguments"`. That statement refers to the older `SIGNAL` / `SLOT` macros but, as far as I'm aware, the same applies to the newer syntax. – G.M. May 03 '23 at 12:08

0 Answers0