34

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 QApplication::closeAllWindows() 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->setShortcuts(QKeySequence::Quit);
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 {
    Q_OBJECT
public slots:
    static void foo();
private:
    Some();
};

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.

Update:

In Qt 5 it is possible.

bartolo-otrit
  • 2,396
  • 3
  • 32
  • 50

3 Answers3

26

Update for QT5: Yes you can

static void someFunction() {
    qDebug() << "pressed";
}
// ... somewhere else
QObject::connect(button, &QPushButton::clicked, someFunction);

In QT4 you can't:

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();

Edit:

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.

Stormenet
  • 25,926
  • 9
  • 53
  • 65
UmNyobe
  • 22,539
  • 9
  • 61
  • 90
  • Yes it is. I don't understand why is it impossible to invoke static function without object in Qt Meta-Object System? – bartolo-otrit Feb 24 '12 at 09:23
  • I agree is it a drawback of the Qt system, still it is not possible. – UmNyobe Feb 24 '12 at 09:36
  • 6
    Can't see much of a drawback. Static functions with side-effects are evil anyway, and one can use a singleton QObject instances if need be. – Frank Osterfeld Feb 24 '12 at 13:13
  • @FrankOsterfeld My problem with it is, when coupled with the fact that you can't declare a QObject in a cpp file, it requires adding 2 files to your project just to subscribe to a signal. – sashoalm Nov 13 '13 at 07:27
  • 1
    @sashoalm: If you're using Qt 5 and C++11 features, you could now use signals with lambdas. – Frank Osterfeld Nov 13 '13 at 11:31
  • The function must not necessarily be static, see answer from ManuelSchneid3r – Anonymous Nov 29 '21 at 10:07
5

It is. (With Qt5)

#include <QApplication>
#include <QDebug>

void foo(){
    qDebug() << "focusChanged";
}


int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    QObject::connect(&app, &QApplication::focusChanged, foo);
    return app.exec();
}
ManuelSchneid3r
  • 15,850
  • 12
  • 65
  • 103
1

In earlier versions of Qt, although you cannot do so as mentioned by @UmNyobe but you can do something like this if you really want to call that static slot :

connect(&object, SIGNAL(some()), this, SLOT(foo()));

void foo()
{
    .... //call your static function here.
}
Ashish Bansal
  • 79
  • 1
  • 6