summaryrefslogtreecommitdiffstats
path: root/examples/corelib/bindableproperties/bindablesubscription
diff options
context:
space:
mode:
Diffstat (limited to 'examples/corelib/bindableproperties/bindablesubscription')
-rw-r--r--examples/corelib/bindableproperties/bindablesubscription/CMakeLists.txt48
-rw-r--r--examples/corelib/bindableproperties/bindablesubscription/bindablesubscription.cpp12
-rw-r--r--examples/corelib/bindableproperties/bindablesubscription/bindablesubscription.h4
-rw-r--r--examples/corelib/bindableproperties/bindablesubscription/bindableuser.h12
-rw-r--r--examples/corelib/bindableproperties/bindablesubscription/main.cpp50
5 files changed, 47 insertions, 79 deletions
diff --git a/examples/corelib/bindableproperties/bindablesubscription/CMakeLists.txt b/examples/corelib/bindableproperties/bindablesubscription/CMakeLists.txt
index bfc794b4b5..2734f44a17 100644
--- a/examples/corelib/bindableproperties/bindablesubscription/CMakeLists.txt
+++ b/examples/corelib/bindableproperties/bindablesubscription/CMakeLists.txt
@@ -1,51 +1,15 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-cmake_minimum_required(VERSION 3.16)
-project(bindablesubscription LANGUAGES CXX)
-
-set(CMAKE_AUTOMOC ON)
-set(CMAKE_AUTOUIC ON)
-
-if(NOT DEFINED INSTALL_EXAMPLESDIR)
- set(INSTALL_EXAMPLESDIR "examples")
-endif()
-
-set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/corelib/bindableproperties/bindablesubscription")
-
-find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)
-
qt_add_executable(bindablesubscription
- ../shared/subscriptionwindow.cpp ../shared/subscriptionwindow.h ../shared/subscriptionwindow.ui
main.cpp
- bindablesubscription.cpp bindablesubscription.h
- bindableuser.cpp bindableuser.h
+ bindablesubscription.cpp
+ bindablesubscription.h
+ bindableuser.cpp
+ bindableuser.h
)
-target_link_libraries(bindablesubscription PUBLIC
- Qt::Core
- Qt::Gui
- Qt::Widgets
+target_link_libraries(bindablesubscription PRIVATE
+ bindableproperties_shared
)
-# Resources:
-set(countries_resource_files
- "../shared/finland.png"
- "../shared/germany.png"
- "../shared/norway.png"
-)
-
-qt_add_resources(bindablesubscription "countries"
- PREFIX
- "/"
- BASE
- "../shared"
- FILES
- ${countries_resource_files}
-)
-
-install(TARGETS bindablesubscription
- RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
- BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
- LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
-)
diff --git a/examples/corelib/bindableproperties/bindablesubscription/bindablesubscription.cpp b/examples/corelib/bindableproperties/bindablesubscription/bindablesubscription.cpp
index 1b65c87f5f..32f4194635 100644
--- a/examples/corelib/bindableproperties/bindablesubscription/bindablesubscription.cpp
+++ b/examples/corelib/bindableproperties/bindablesubscription/bindablesubscription.cpp
@@ -10,10 +10,11 @@ BindableSubscription::BindableSubscription(BindableUser *user) : m_user(user)
{
Q_ASSERT(user);
- m_price.setBinding([this] { return qRound(calculateDiscount() * m_duration * basePrice()); });
+ m_price.setBinding(
+ [this] { return qRound(calculateDiscount() * int(m_duration) * basePrice()); });
m_isValid.setBinding([this] {
- return m_user->country() != BindableUser::None && m_user->age() > 12;
+ return m_user->country() != BindableUser::Country::AnyCountry && m_user->age() > 12;
});
}
@@ -38,14 +39,13 @@ double BindableSubscription::calculateDiscount() const
case Yearly:
return 0.6;
}
- Q_ASSERT(false);
- return -1;
+ Q_UNREACHABLE_RETURN(-1);
}
int BindableSubscription::basePrice() const
{
- if (m_user->country() == BindableUser::None)
+ if (m_user->country() == BindableUser::Country::AnyCountry)
return 0;
- return (m_user->country() == BindableUser::Norway) ? 100 : 80;
+ return (m_user->country() == BindableUser::Country::Norway) ? 100 : 80;
}
diff --git a/examples/corelib/bindableproperties/bindablesubscription/bindablesubscription.h b/examples/corelib/bindableproperties/bindablesubscription/bindablesubscription.h
index b135618bec..03870d0617 100644
--- a/examples/corelib/bindableproperties/bindablesubscription/bindablesubscription.h
+++ b/examples/corelib/bindableproperties/bindablesubscription/bindablesubscription.h
@@ -4,7 +4,7 @@
#ifndef BINDABLESUBSCRIPTION_H
#define BINDABLESUBSCRIPTION_H
-#include <QPointer>
+#include <QBindable>
#include <QProperty>
class BindableUser;
@@ -14,7 +14,7 @@ class BindableUser;
class BindableSubscription
{
public:
- enum Duration { Monthly = 1, Quarterly = 4, Yearly = 12 };
+ enum Duration { Monthly = 1, Quarterly = 3, Yearly = 12 };
BindableSubscription(BindableUser *user);
BindableSubscription(const BindableSubscription &) = delete;
diff --git a/examples/corelib/bindableproperties/bindablesubscription/bindableuser.h b/examples/corelib/bindableproperties/bindablesubscription/bindableuser.h
index b7e44d52ec..6bb9bcdcb5 100644
--- a/examples/corelib/bindableproperties/bindablesubscription/bindableuser.h
+++ b/examples/corelib/bindableproperties/bindablesubscription/bindableuser.h
@@ -4,6 +4,8 @@
#ifndef BINDABLEUSER_H
#define BINDABLEUSER_H
+#include <QBindable>
+#include <QLocale>
#include <QProperty>
//! [bindable-user-class]
@@ -11,13 +13,9 @@
class BindableUser
{
public:
- enum Country {
- None,
- Finland,
- Germany,
- Norway,
- };
+ using Country = QLocale::Territory;
+public:
BindableUser() = default;
BindableUser(const BindableUser &) = delete;
@@ -30,7 +28,7 @@ public:
QBindable<int> bindableAge() { return &m_age; }
private:
- QProperty<Country> m_country { None };
+ QProperty<Country> m_country { QLocale::AnyTerritory };
QProperty<int> m_age { 0 };
};
diff --git a/examples/corelib/bindableproperties/bindablesubscription/main.cpp b/examples/corelib/bindableproperties/bindablesubscription/main.cpp
index c861f8af4c..466f487b8e 100644
--- a/examples/corelib/bindableproperties/bindablesubscription/main.cpp
+++ b/examples/corelib/bindableproperties/bindablesubscription/main.cpp
@@ -6,11 +6,15 @@
#include "bindableuser.h"
#include <QApplication>
-#include <QButtonGroup>
+#include <QBindable>
#include <QLabel>
+#include <QLocale>
#include <QPushButton>
#include <QRadioButton>
#include <QSpinBox>
+#include <QString>
+
+using namespace Qt::StringLiterals;
int main(int argc, char *argv[])
{
@@ -19,46 +23,48 @@ int main(int argc, char *argv[])
BindableSubscription subscription(&user);
SubscriptionWindow w;
+ // clazy:excludeall=lambda-in-connect
+ // when subscription is out of scope so is window
// Initialize subscription data
- QRadioButton *monthly = w.findChild<QRadioButton *>("btnMonthly");
- QObject::connect(monthly, &QRadioButton::clicked, [&] {
+ QRadioButton *monthly = w.findChild<QRadioButton *>(u"btnMonthly"_s);
+ QObject::connect(monthly, &QRadioButton::clicked, monthly, [&] {
subscription.setDuration(BindableSubscription::Monthly);
});
- QRadioButton *quarterly = w.findChild<QRadioButton *>("btnQuarterly");
- QObject::connect(quarterly, &QRadioButton::clicked, [&] {
+ QRadioButton *quarterly = w.findChild<QRadioButton *>(u"btnQuarterly"_s);
+ QObject::connect(quarterly, &QRadioButton::clicked, quarterly, [&] {
subscription.setDuration(BindableSubscription::Quarterly);
});
- QRadioButton *yearly = w.findChild<QRadioButton *>("btnYearly");
- QObject::connect(yearly, &QRadioButton::clicked, [&] {
+ QRadioButton *yearly = w.findChild<QRadioButton *>(u"btnYearly"_s);
+ QObject::connect(yearly, &QRadioButton::clicked, yearly, [&] {
subscription.setDuration(BindableSubscription::Yearly);
});
// Initialize user data
- QPushButton *germany = w.findChild<QPushButton *>("btnGermany");
- QObject::connect(germany, &QPushButton::clicked, [&] {
- user.setCountry(BindableUser::Germany);
+ QPushButton *germany = w.findChild<QPushButton *>(u"btnGermany"_s);
+ QObject::connect(germany, &QPushButton::clicked, germany, [&] {
+ user.setCountry(BindableUser::Country::Germany);
});
- QPushButton *finland = w.findChild<QPushButton *>("btnFinland");
- QObject::connect(finland, &QPushButton::clicked, [&] {
- user.setCountry(BindableUser::Finland);
+ QPushButton *finland = w.findChild<QPushButton *>(u"btnFinland"_s);
+ QObject::connect(finland, &QPushButton::clicked, finland, [&] {
+ user.setCountry(BindableUser::Country::Finland);
});
- QPushButton *norway = w.findChild<QPushButton *>("btnNorway");
- QObject::connect(norway, &QPushButton::clicked, [&] {
- user.setCountry(BindableUser::Norway);
+ QPushButton *norway = w.findChild<QPushButton *>(u"btnNorway"_s);
+ QObject::connect(norway, &QPushButton::clicked, norway, [&] {
+ user.setCountry(BindableUser::Country::Norway);
});
- QSpinBox *ageSpinBox = w.findChild<QSpinBox *>("ageSpinBox");
- QObject::connect(ageSpinBox, &QSpinBox::valueChanged, [&](int value) {
- user.setAge(value);
- });
+ QSpinBox *ageSpinBox = w.findChild<QSpinBox *>(u"ageSpinBox"_s);
+ QBindable<int> ageBindable(ageSpinBox, "value");
+ user.bindableAge().setBinding([ageBindable](){ return ageBindable.value();});
- QLabel *priceDisplay = w.findChild<QLabel *>("priceDisplay");
+ QLabel *priceDisplay = w.findChild<QLabel *>(u"priceDisplay"_s);
// Track price changes
//! [update-ui]
auto priceChangeHandler = subscription.bindablePrice().subscribe([&] {
- priceDisplay->setText(QString::number(subscription.price()));
+ QLocale lc{QLocale::AnyLanguage, user.country()};
+ priceDisplay->setText(lc.toCurrencyString(subscription.price() / subscription.duration()));
});
auto priceValidHandler = subscription.bindableIsValid().subscribe([&] {