diff --git a/src/counter.cpp b/src/counter.cpp index b141c7e..bdd5a8e 100644 --- a/src/counter.cpp +++ b/src/counter.cpp @@ -1,18 +1,34 @@ #include "counter.h" +#include Counter::Counter(QObject *parent) - : QObject(parent) + : QObject{parent} + , m_value{0} { // Nothing } -uint Counter::counter() const +quint32 Counter::value() const { - return m_counter; + return m_value; } -void Counter::setCounter(const uint counter) +void Counter::setValue(const quint32 value) { - m_counter = counter; - Q_EMIT counterChanged(); + m_value = value; + Q_EMIT valueChanged(); +} + +void Counter::increment() { + if (m_value == UINT_MAX) return; + + m_value += 1; + Q_EMIT valueChanged(); +} + +void Counter::decrement() { + if (m_value == 0) return; + + m_value -= 1; + Q_EMIT valueChanged(); } diff --git a/src/counter.h b/src/counter.h index 57cddc9..223c44b 100644 --- a/src/counter.h +++ b/src/counter.h @@ -5,18 +5,22 @@ #include class Counter: public QObject { - Q_OBJECT - Q_PROPERTY(uint counter READ counter WRITE setCounter NOTIFY counterChanged) - QML_ELEMENT -public: - explicit Counter(QObject *parent = nullptr); + Q_OBJECT + Q_PROPERTY(uint value READ value WRITE setValue NOTIFY valueChanged) + QML_ELEMENT - [[nodiscard]] uint counter() const; - void setCounter(const uint counter); +public: + explicit Counter(QObject *parent = nullptr); + + [[nodiscard]] quint32 value() const; + void setValue(const quint32 value); + + Q_INVOKABLE void increment(); + Q_INVOKABLE void decrement(); Q_SIGNALS: - void counterChanged(); + void valueChanged(); private: - uint m_counter; + quint32 m_value; }; diff --git a/src/qml/Main.qml b/src/qml/Main.qml index 874b379..37ec7b4 100644 --- a/src/qml/Main.qml +++ b/src/qml/Main.qml @@ -2,6 +2,7 @@ import QtQuick import QtQuick.Controls as Controls import QtQuick.Layouts import org.kde.kirigami as Kirigami +import fr.mrdev023.tutorial_kirigami2 Kirigami.ApplicationWindow { id: root @@ -23,9 +24,26 @@ Kirigami.ApplicationWindow { pageStack.initialPage: Kirigami.ScrollablePage { title: i18nc("@title", "Main Page") - actions: [ - ] + Kirigami.FlexColumn { + anchors.fill: parent - Item {} + Controls.Label { + text: counter.value + } + + Controls.Button { + text: i18n("-") + onClicked: counter.decrement() + } + + Controls.Button { + text: i18n("+") + onClicked: counter.increment() + } + } + } + + Counter { + id: counter } }