First working counter

This commit is contained in:
Florian RICHER 2025-07-04 18:06:41 +02:00
parent 80e1755a8a
commit 6264bf00ce
Signed by: florian.richer
GPG key ID: C73D37CBED7BFC77
3 changed files with 56 additions and 18 deletions

View file

@ -1,18 +1,34 @@
#include "counter.h" #include "counter.h"
#include <climits>
Counter::Counter(QObject *parent) Counter::Counter(QObject *parent)
: QObject(parent) : QObject{parent}
, m_value{0}
{ {
// Nothing // 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; m_value = value;
Q_EMIT counterChanged(); 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();
} }

View file

@ -5,18 +5,22 @@
#include <qtmetamacros.h> #include <qtmetamacros.h>
class Counter: public QObject { class Counter: public QObject {
Q_OBJECT Q_OBJECT
Q_PROPERTY(uint counter READ counter WRITE setCounter NOTIFY counterChanged) Q_PROPERTY(uint value READ value WRITE setValue NOTIFY valueChanged)
QML_ELEMENT QML_ELEMENT
public:
explicit Counter(QObject *parent = nullptr);
[[nodiscard]] uint counter() const; public:
void setCounter(const uint counter); explicit Counter(QObject *parent = nullptr);
[[nodiscard]] quint32 value() const;
void setValue(const quint32 value);
Q_INVOKABLE void increment();
Q_INVOKABLE void decrement();
Q_SIGNALS: Q_SIGNALS:
void counterChanged(); void valueChanged();
private: private:
uint m_counter; quint32 m_value;
}; };

View file

@ -2,6 +2,7 @@ import QtQuick
import QtQuick.Controls as Controls import QtQuick.Controls as Controls
import QtQuick.Layouts import QtQuick.Layouts
import org.kde.kirigami as Kirigami import org.kde.kirigami as Kirigami
import fr.mrdev023.tutorial_kirigami2
Kirigami.ApplicationWindow { Kirigami.ApplicationWindow {
id: root id: root
@ -23,9 +24,26 @@ Kirigami.ApplicationWindow {
pageStack.initialPage: Kirigami.ScrollablePage { pageStack.initialPage: Kirigami.ScrollablePage {
title: i18nc("@title", "Main Page") 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
} }
} }