Cute   Learning Hub


Calculator Tutorial

This tutorial uses a simple Calculator class with a remote slot returning the sum of the two integers given as arguments to illustrate how remote signal-slot connections and direct remote slot calls work. The Calculator class declares the remote signal and slot as follows (the source code is available on GitHub):

// The Calculator class declares the remote addIntegers slot
// and the remote overflow signal
public slots:
    REMOTE_SLOT qint32 addIntegers(qint32 a, qint32 b);

signals:
    REMOTE_SIGNAL void overflow(Integer a, Integer b);

The Calculator class defines the addIntegers slot as follows:

qint32 Calculator::addIntegers(qint32 a, qint32 b)
{
    if ((a > 0 && b > 0 && (a+b) < 0)
        || (a < 0 && b < 0 && (a+b) > 0))
    {
        emit overflow(Integer(a), Integer(b));
        return -1;
    }
    else
        return a+b;
}

If the sum overflows, the remote addIntegers slot emits the remote overflow signal. In this case, the slot returns -1. The remote overflow signal uses the Integer class as a custom type for its arguments.

To use the Integer class as a custom argument in remote signals/slots, we have to:

The simple calculator repo builds two assets: the remote objects library containing the registered Calculator class and the calculator test app. The CMake command below creates these assets (all the source code is available on GitHub):

cmake -DCUTE_SERVER_SDK_DIR=/home/glauco/Programming/Cute/CuteServerSDK/Qt6 \
-DCUTE_CLIENT_SDK_DIR=/home/glauco/Programming/Cute/CuteClientSDK/Qt6 \
-DCMAKE_PREFIX_PATH=/home/glauco/Programming/Qt/SDK/6.2.3/gcc_64 \
/home/glauco/Programming/SimpleCalculator

cmake --build .

The command above builds the remote objects library and the CalculatorClientApp application. Three CMake variables are required: the CUTE_SERVER_SDK_DIR variable containing the directory of the Cute Server SDK (the directory containing the include and lib dirs), the CUTE_CLIENT_SDK_DIR variable containing the directory of the Cute Client SDK, and the CMAKE_PREFIX_PATH containing the Qt's installation directory.

The CalculatorClient class, used by the CalculatorClientApp application, establishes a remote signal-slot connection to the remote overflow signal and calls the addIntegers remote slot directly. If the integer addition overflows, the remote slot emits the overflow signal. In this case, the remote slot returns -1.

The Cute server Startup edition is used in this tutorial to expose the Calculator class to the CalculatorClientApp application.

The Cute server Startup edition can be downloaded on cuteserver.io, and is provided as a self-extractable compressed tar archive built with Makeself. The Cute server Startup edition is installed as follows:

# make the installer executable
chmod a+x ~/Downloads/cute-server-startup.run
# install Cute server Startup edition in ~/Cute/Server/Startup
~/Downloads/cute-server-startup.run --target ~/Cute/Server/Startup

The Cute server uses a text file with the INI format for configuration. We use a file with the contents shown below as the configuration file for the Cute server Startup edition to make the server listen for connections on 127.0.100.125 on port 1234:

[Common]
    logTo = syslog
    logLevel = Info
    remoteObjectsLib = /full/path/to/libRemoteObjectsLib.so
    workerCount = 1

[Listener.Tutorial]
    address = 127.0.100.125
    port = 1234

All Cute server editions allow evaluation on AWS EC2/Lightsail for 30 days. Otherwise, a license is required, and users must specify them in the configuration file. For example, if the file license.txt contains a purchased subscription license, then the configuration file specifies the purchased license as follows:

[Common]
    licenseFilePath = /location/of/license.txt
    logTo = syslog
    logLevel = Info
    remoteObjectsLib = /full/path/to/libRemoteObjectsLib.so
    workerCount = 1

[Listener.Tutorial]
    address = 127.0.100.125
    port = 1234

The command below starts the server:

~/Cute/Server/Startup/bin/cute-server-startup
    -f /tmp/cute-server.config

The cute-server.config file in the above command is the configuration file with the INI format we showed previously.

Now, if we run the CalculatorClientApp application, it interacts with the server and uses remote objects of the Calculator class to perform calculations remotely through remote signals and slots.

The CalculatorClientApp fetches the integers to be added from command-line arguments as shown below:

./CalculatorClientApp a=3 b=5
Sum is 8.
./CalculatorClientApp a=3 b=22
Sum is 25.
./CalculatorClientApp a=3 b=2147483647
Adding 3 to 2147483647 overflows.
Sum is -1.
./CalculatorClientApp a=5 b=2147483647
Adding 5 to 2147483647 overflows.
Sum is -1.

The Cute servers and SDKs enable seamless network-based interactions through remote signals and slots. They abstract away from developers everything related to network programming. Minimal changes are required, as only a set of macros are needed to tag signals and slots as remote and register remote object classes to the Cute server.

The Cute servers and SDKs are available on cuteserver.io, and the Calculator source code is available on GitHub.