Project

General

Profile

Using Wt::Signal as a simple widget synchronization mecha... ยป main.cpp

WtSignalTest - Michele Perrone, 11/07/2024 09:56 AM

 
#include <Wt/WApplication.h>
#include <Wt/WContainerWidget.h>
#include <Wt/WSlider.h>
#include <Wt/WSignal.h>

// Global state variable
int globalState = 0;

// Signal for global state changes
Wt::Signal<int> globalStateChanged;

class MyApplication : public Wt::WApplication
{
public:
MyApplication(const Wt::WEnvironment& env)
: Wt::WApplication(env)
{
// Create a container to hold the slider
auto container = root()->addWidget(std::make_unique<Wt::WContainerWidget>());

// Create the slider
auto slider = container->addWidget(std::make_unique<Wt::WSlider>());
slider->setMinimum(0);
slider->setMaximum(100);
slider->setTickInterval(5);
slider->setTickPosition(Wt::WSlider::TicksBothSides);
slider->resize(300, 50);
slider->setValue(globalState);

// Connect the slider value to the global state
slider->valueChanged().connect([](int value) {
globalState = value;
globalStateChanged.emit(globalState);
fprintf(stderr, "globalStateChanged.connect(): emitted value %i\n", value);
});

// Update the slider whenever the global state changes
globalStateChanged.connect([slider](int state) {
slider->setValue(state);
fprintf(stderr, "globalStateChanged.connect(): received value %i\n", state);
});
}
};

int main(int argc, char **argv)
{
return Wt::WRun(argc, argv, [](const Wt::WEnvironment& env) {
return std::make_unique<MyApplication>(env);
});
}
    (1-1/1)