KDBindings API Documentation  1.0.95
07-advanced-connections/main.cpp

An example of how to use the KDBindings::Signal::connect() overloaded function for advanced slot connections.

The output of this example is:

Hello World!
Emitted value: 5
true
/*
This file is part of KDBindings.
SPDX-FileCopyrightText: 2021 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
Author: Leon Matthes <leon.matthes@kdab.com>
SPDX-License-Identifier: MIT
Contact KDAB at <info@kdab.com> for commercial licensing options.
*/
#include <ios>
#include <iostream>
#include <string>
using namespace KDBindings;
void display()
{
std::cout << "Hello World!" << std::endl;
}
void displayLabelled(const std::string &label, int value)
{
std::cout << label << ": " << value << std::endl;
}
class SignalHandler
{
public:
bool received = 0;
void receive()
{
received = true;
}
};
int main()
{
Signal<int> signal;
std::vector<ConnectionHandle> connections;
// Signal::connect allows connecting functions that take too few arguments.
connections.emplace_back(signal.connect(display));
// As well as functions with too many arguments, as long as default values are provided.
connections.emplace_back(signal.connect(displayLabelled, "Emitted value"));
// This is very useful to connect member functions, where the first implicit argument
// is a pointer to the "this" object.
SignalHandler handler;
connections.emplace_back(signal.connect(&SignalHandler::receive, &handler));
// This will print "Hello World!" and "Emitted value: 5" in an unspecified order.
// It will also set handler.received to true
signal.emit(5);
std::cout << std::boolalpha << handler.received << std::endl;
for (auto &connection : connections) {
connection.disconnect();
}
return 0;
}
The main namespace of the KDBindings library.
Definition: binding.h:21

© Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/
KDBindings
Reactive programming & data binding in C++
https://github.com/KDAB/KDBindings/
Generated by doxygen 1.9.1