bartolo-otrit bartolo-otrit - 8 months ago 104
C++ Question

Is it possible to connect a signal to a static slot without a receiver instance?

Is it possible to connect a signal to static slot without receiver instance?

Like this:

connect(&object, SIGNAL(some()), STATIC_SLOT(staticFooMember()));

There is a
function with [static slot] attribute in Qt documentation. And there is an example of using it from the documentation:

exitAct = new QAction(tr("E&xit"), this);
exitAct->setStatusTip(tr("Exit the application"));
connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));

Is it allowed to do the same action but without passing an instance variable (e.g. when a class has only static functions)?

class Some : public QObject {
public slots:
static void foo();

Maybe Frank Osterfeld is right and it is better to use singleton pattern in this case but I am still surprised why this feature has not been implemented yet.


In Qt 5 it is possible.


No it is not allowed. Rather, it is allowed to use a slot which is a static function, but to be able to connect it you need an instance.

In their example,

connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));

means than they previously called

QApplication* qApp = QApplication::instance();


The only interface for connecting object is the function

bool QObject::connect ( const QObject * sender, const QMetaMethod & signal, const QObject * receiver, const QMetaMethod & method, Qt::ConnectionType type = Qt::AutoConnection )

How are you going to get rid of const QObject * receiver?

Check the moc files in your project, it speaks by itself.