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 <climits>
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();
}

View file

@ -5,18 +5,22 @@
#include <qtmetamacros.h>
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;
};

View file

@ -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
}
}