Alexis Lopez Zubieta
Alexis Lopez Zubieta

Reputation: 81

Connect QObject interface signal to a lambda Slot

I'm trying to connect a QObject signal to a lambda slot but using an interface pointer to the object instead of a pointer to the concrete QObject class. But I get this wierd error:

 error: no matching function for call to ‘FileSystemModel::connect(model_filesystem::Directory*&, const char*, FileSystemModel::setDirectory(model_filesystem::Directory*)::<lambda()>)’
 });

Here are some snippets of my code:

// Interface declaration
namespace model_filesystem {

class Directory {
public:

    virtual ~Directory()
    virtual QString name() = 0;

Q_SIGNALS:
        void changed();
        void failure(QString msg);
    };
}

Q_DECLARE_INTERFACE(model_filesystem::Directory, "org.moonlightde.panel.model_filesystem.Directory/1.0")

//Implementation
class GVFSDirectory : public QObject,  public model_filesystem::Directory {
    Q_OBJECT
    Q_INTERFACES(model_filesystem::Directory)
public:
    GVFSDirectory(const QString &uri);
    GVFSDirectory(GFile * gfile);

    virtual ~GVFSDirectory();

    virtual QString name();
public Q_SLOTS:
    void update();

Q_SIGNALS:
    void changed();
    void failure(QString msg);

// Usage
Directory * directory = new GVFSDirectory("/");
connect(directory, SIGNAL(model_filesystem::Directory::changed()), [this] () {
    setupModel();
});

Upvotes: 0

Views: 633

Answers (1)

James Adkison
James Adkison

Reputation: 9602

error: no matching function for call to ‘FileSystemModel::connect(model_filesystem::Directory ...

The Directory class is not a QObject. It needs to subclass QObject if it's going to use signals and/or slots.

From Qt5 - Signals & Slots:

All classes that inherit from QObject or one of its subclasses (e.g., QWidget) can contain signals and slots.

From Qt5 - Plug & Paint Example:

To make it possible to query at run-time whether a plugin implements a given interface, we must use the Q_DECLARE_INTERFACE() macro.

This seems to provide the machanism to query at run-time whether a plugin implements a given interface so I don't see why there should be any expectation the Directory class should work like a QObject without subclassing it.

In other words, it's fine to use this without subclassing QObject but this doesn't grant Directory the ability to use signals and/or slots.

SIGNAL(model_filesystem::Directory::update())

Even if Directory was a QObject there is no update signal, only changed and failure.

Upvotes: 1

Related Questions