No matching function for call to QObject::connect



  • I'm trying to compile this code inside a constructor of a class named MyClass:

    QTimer *timer = new QTimer;
    timer->setInterval(0);
    QObject::connect(timer, &QTimer::timeout, [this](){
        //Do some stuff
    });
    timer->start();
    

    This compiles just fine in Visual Studio on Windows. But with GCC on Linux it gives the error "no matching function for call to 'MyClass::connect(QTimer*&, void(QTimer::*)(QTimer::QPrivateSignal), MyClass::MyClass()::<lambda()>)'. What should I do?



  • @Donald-Duck It's probably because QTimer::timeout is a private signal, the new style syntax can't access it. If that's the case you have to use the old SIGNAL/SLOT syntax without lambda.



  • @Donald-Duck
    Regarding to the 0-timer I don't know why you need to have a 0-timer in a constructor, but in the doc of QTimer it says to 0-timers http://doc.qt.io/qt-5/qtimer.html#details:

    This is the traditional way of implementing heavy work in GUI applications, but as multithreading is nowadays becoming available on more and more platforms, we expect that zero-millisecond QTimer objects will gradually be replaced by QThreads.

    Otherwise you can use the startTimer() function of the QObject class


  • Lifetime Qt Champion

    Hi,

    Which version of Qt are you using in each OS ?

    Also, aren't you missing #include <QTimer> in your .cpp file ?



  • @Donald-Duck Probably, you are using older version of Qt on your Linux that doesn't understand the newer format of QObject::connect statement. Try with older format.



  • I found the problem. I had to activate C++11, which is not activated be default. In Qt Creator, I had to add the following line:

    CONFIG += c++11
    


  • @Donald-Duck said in No matching function for call to QObject::connect:

    I found the problem. I had to activate C++11, which is not activated be default. In Qt Creator, I had to add the following line:

    CONFIG += c++11
    

    Note that this behaviour is gcc's default, QtCreator is simply an IDE, not a compiler. Using a .pro file without this config with VS 2013+ would most probably have worked.


Log in to reply