4 ms·
With Qt you don't really have to deal with unexpected messages. A signal is triggered by an event, like a button click or a slider value changing, a checkbox be
by thamer 5y ago
With Qt you don't really have to deal with unexpected messages. A signal is triggered by an event, like a button click or a slider value changing, a checkbox being toggled, this kind of thing. A slot is just a place to receive it, it's a regular function or method with a compatible signature for the kind of event it's meant to process. There are no unexpected calls because you have to explicitly connect a signal to a slot, something like:
QObject::connect(&button, &QPushButton::clicked,
&mainWindowController, &MainWindowController::onButtonClicked);
If no slot is connected, the signal just doesn't go anywhere.
Something that's special with Qt is that signals and slots are declared in the class using custom syntax, e.g.
slots:
void onValueChanged(int value);
signals:
void valueChanged(int newValue);
Yes here "signals:" and "slots:" look like the same kind of keyword as "public:" or "private:" in a class declaration, even though internally they're #defined as "public:" and "/**/" respectively.
Qt makes use of a tool called MOC – the Meta Object Compiler – to generate code for signals and slots. It's not a pre-processor but a code generator, which has to run before you can actually compile the code. I think you could probably implement a similar system of event subscriptions without this additional code generation step.
See this page for details: https://woboq.com/blog/how-qt-signals-slots-work.html https://woboq.com/blog/how-qt-signals-slots-work.html
- PaulDavisThe1st 5y agosignals and slots are just an anonymous callback system. There's no more (or less) magic going on. Anonymous? That means that the thing initiating the callback has no clue what the objects are that will execute callbacks. Gtkmm, or rather its dependent library libsigc++, does not require a separate build step like MOC, it can handle signals and slots "natively" because it used a more modern version of C++. Qt required MOC when it was first written, and they never moved away from it, despite the language having moved on to allow the required functionality all by itself.