Accessing signals and slots in scripts

QJSEngine also offers the capability to use signals and slots. The slot can be either a C++ method or a JavaScript function. The connection can be made either in C++ or in the script.

First, let's see how to establish a connection within a script. When a QObject instance is exposed to a script, the object's signals become the properties of the wrapping object. These properties have a connect method that accepts a function object that is to be called when the signal is emitted. The receiver can be a regular slot or a JavaScript function. The most common case is when you connect the signal to an anonymous function:

pushButton.toggled.connect(function() {
    console.log('button toggled!');
});

If you need ...

Get Game Programming using Qt 5 Beginner's Guide - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.