Skip to content

04-simple-property/main.cpp

An example of how to create a KDBindings::Property and use its valueChanged() KDBindings::Signal to receive notifications whenever the value of the KDBindigns::Property changes.

The output of this example is:

1
2
3
The new value is 42
The new value is 69
Property value is 69
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
  This file is part of KDBindings.

  SPDX-FileCopyrightText: 2021-2022 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
  Author: Sean Harmer <sean.harmer@kdab.com>

  SPDX-License-Identifier: MIT

  Contact KDAB at <info@kdab.com> for commercial licensing options.
*/

#include <kdbindings/property.h>

#include <iostream>
#include <string>

using namespace KDBindings;

class Widget
{
public:
    Widget(std::string const &name)
        : value(0)
        , m_name(name)
    {
    }

    Property<int> value;

private:
    std::string m_name;
};

int main()
{
    Widget w("A cool widget");

    w.value.valueChanged().connect([](int newValue) {
        std::cout << "The new value is " << newValue << std::endl;
    });

    w.value = 42;
    w.value = 69;

    std::cout << "Property value is " << w.value << std::endl;

    return 0;
}

Filename: 04-simple-property/main.cpp


Updated on 2022-04-12 at 00:03:57 +0000

Back to top