NodeGui allows you to listen to various events that might originate from the underlying Qt widgets. These events can either be a simple button click or a text change on a lineedit or even something like window being hidden and shown.
In order to do this we need to attach an event listener to the respective widget.
Technically, the event listener is a NodeJs EventEmitter instance that listens to events from the underlying Qt widget. The native Qt widget would send all the events to the event emitter in NodeGui world and the user can essentially subscribe to it.
Lets see an example to see how this looks in practice.
The following example demonstrates how to add an event listener to a button widget.
The addEventListener function accepts an event type, followed by a handler for the event. You can register multiple handlers for the same event by calling addEventListener multiple times.
Internally, Qt widgets in nodegui has two types of events.:
- Signals: In short these are basically different for different widgets. So a button maybe have
pressedsignal, while a linedit may have
- QEvents: These are common set of events for all the widgets/qobjects in NodeGui world. These are also helpful at times but typically you would end up using signals more than these common events.
In NodeGui you can listen to both Signals and QEvents using the same addEventListener function.
How do I know which events are supported ?
In order to find all the supported events for a widget you can take a look at
All Signals for the widgets:
You can subscribe to a signal like so:
The value you receive in the callback depends on the signal. Refer to respective signal docs for more details. All the handlers are also typed. So if you are using typescript you should get correct autocomplete for it.
All common QEvents for the widgets
In nodegui all these common QEvents are represented under an enum type: WidgetEventTypes
You can subscribe to a QEvent like so:
Note here that every QEvent handler gives a reference to native QEvent in the handler callback. Not all native QEvent wrappers are implemented yet and we might need your help regarding those. Feel free to jump in and contribute to the nodegui core.