0

Is there a way to connect signal and slot without using connect function?

If a way exists, please give some examples.

Melebius
  • 6,183
  • 4
  • 39
  • 52
lens
  • 133
  • 1
  • 11

2 Answers2

4

Nope, there is no other way, not in the public API at least. In Qt4 there is only the connect() function with the SIGNAL() and SLOT macro().

In Qt5 you have another, type-safe connection syntax, but it still uses the connect() function. And in QML you can use "attached handlers" - onSignal: doStuff() - but that's just for QML.

dtech
  • 47,916
  • 17
  • 112
  • 190
  • I have seen some code that emit a signal, but I don't seen connect. – lens Aug 10 '17 at 09:55
  • The connection can be anywhere else. Connecting and emitting are completely different things. It may even happen automatically in some constructor or something like that. – dtech Aug 10 '17 at 09:56
  • Also, you can emit a signal that is not connected to anything, it that case it will simply not do anything. Signals are only there for potential listeners. They have to be emitted regardless of whether anyone is connected or not. Because the type that implements the signal has no knowledge of who is going to eventually connect. – dtech Aug 10 '17 at 09:57
  • If I emit a signal, how to let another function handle this signal. – lens Aug 10 '17 at 10:00
  • You are not concerned with who connects, you are concerned with notifying any potential listeners. That's up to the connected function. When a signal is emitted, all connected slots will be executed in order. – dtech Aug 10 '17 at 10:02
2

There is a way to use the meta-call interface to avoid using the connect() function. Depending on what you precisely need to do though this may not be the solution you are looking for, or it may be.

You essentially define a callback slot function in your QOBJECT class with a certain syntax and let the MOC sort the 'connection' out.

So say you wanted to use:

connect(ui->actionButton, SIGNAL(triggered()), this, SLOT(someCallbackSlot()));

but without having to use the callback function...

You can do it implicitly with a particular syntax of a member function in the QOBJECT who receives the signal:

class MyQtObj : public QWidget
{
QObject

private slots:

void on_actionButton_triggered();

}

Where on_actionButton_triggered is the functional equivalent of someCallbackSlot(), actionButton is the name of the button/action/signal emitter and triggered() is the signal emitted.

So any of these functions are valid providing the signal emitter is correct:

void on_minimizedButton_clicked();

void on_closeButton_released();

etc

When you run the Qt Meta-Object-Compiler and generate your moc_.cpp file of this class there will be a function called qt_static_metacall; this contains a switch statement that looks like :

void MyQtObj::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)
{
    if (_c == QMetaObject::InvokeMetaMethod) {
        MyQtObj*_t = static_cast<MyQtObj*>(_o);
        Q_UNUSED(_t)
        switch (_id) {
        case 0: _t->on_actionButton_triggered(); break;
        default: ;
        }
    }
}

If you put a break point in this and trigger your action/button/signal you should see your function get executed.

I'm not too sure of the details of how this works though and I noticed it when I used this frame-less Qt window code and have now adopted it in my own Qt C++ projects.