c++qtlambdaqt-signalsqfilesystemwatcher

Cannot connect QFileSystemWatcher::directoryChanged to a lambda


As the title says, I'm trying to connect the QFileSystemWatcher::directoryChanged(const QString&) signal to a lambda, but when I compile it with g++ (7.2.1), I get the following error (abbreviated since SO won't let me post too much code):

g++ -c -pipe -O2 -std=gnu++1y -Wall -W -D_REENTRANT -fPIC -DQT_DEPRECATED_WARNINGS -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I. -I. -isystem /usr/include/qt5 -isystem /usr/include/qt5/QtGui -isystem /usr/include/qt5/QtCore -I. -isystem /usr/include/libdrm -I/usr/lib64/qt5/mkspecs/linux-g++ -o foo.o foo.cpp
foo.cpp: In function ‘int main()’:
foo.cpp:10:55: error: no matching function for call to ‘QObject::connect(QFileSystemWatcher (&)(), void (QFileSystemWatcher::*)(const QString&, QFileSystemWatcher::QPrivateSignal), main()::<lambda(const QString&)>)’
                      [] (const QString&) { return 0; });

I'm compiling with Qt 5.9. Any ideas why this is happening? AFAICT the lambda I'm using is fine. This is my MWE:

foo.cpp:

#include <QObject>
#include <QFileSystemWatcher>

int main()
{
    QFileSystemWatcher watcher();
    QObject::connect(watcher, &QFileSystemWatcher::directoryChanged,
                     [] (const QString&) { return 0; });
}

foo.pro:

######################################################################
# Automatically generated by qmake (3.1) Thu Dec 28 11:11:15 2017
######################################################################

TEMPLATE = app
TARGET = foo
INCLUDEPATH += .
CONFIG += c++14
DEFINES += QT_DEPRECATED_WARNINGS

# Input
SOURCES += foo.cpp

Solution

  • Check the type of the first argument the Qt 5.9 API states that it has to be a pointer to a QObject derived Object.

    connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)