summaryrefslogtreecommitdiffstats
path: root/examples/corelib/bindableproperties
diff options
context:
space:
mode:
Diffstat (limited to 'examples/corelib/bindableproperties')
-rw-r--r--examples/corelib/bindableproperties/CMakeLists.txt35
-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
-rw-r--r--examples/corelib/bindableproperties/doc/src/bindableproperties.qdoc35
-rw-r--r--examples/corelib/bindableproperties/shared/CMakeLists.txt23
-rw-r--r--examples/corelib/bindableproperties/subscription/CMakeLists.txt43
-rw-r--r--examples/corelib/bindableproperties/subscription/main.cpp38
-rw-r--r--examples/corelib/bindableproperties/subscription/subscription.cpp8
-rw-r--r--examples/corelib/bindableproperties/subscription/subscription.h2
-rw-r--r--examples/corelib/bindableproperties/subscription/user.h13
13 files changed, 154 insertions, 169 deletions
diff --git a/examples/corelib/bindableproperties/CMakeLists.txt b/examples/corelib/bindableproperties/CMakeLists.txt
index c6d9076fd8..07a2b7a2cf 100644
--- a/examples/corelib/bindableproperties/CMakeLists.txt
+++ b/examples/corelib/bindableproperties/CMakeLists.txt
@@ -1,2 +1,33 @@
-qt_internal_add_example(bindablesubscription)
-qt_internal_add_example(subscription)
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+cmake_minimum_required(VERSION 3.16)
+project(bindableproperties LANGUAGES CXX)
+
+find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)
+
+qt_standard_project_setup()
+
+add_subdirectory(shared)
+add_subdirectory(subscription)
+add_subdirectory(bindablesubscription)
+
+install(TARGETS subscription bindablesubscription
+ BUNDLE DESTINATION .
+ RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
+ LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
+)
+
+qt_generate_deploy_app_script(
+ TARGET subscription
+ OUTPUT_SCRIPT deploy_script
+ NO_UNSUPPORTED_PLATFORM_ERROR
+)
+install(SCRIPT ${deploy_script})
+
+qt_generate_deploy_app_script(
+ TARGET bindablesubscription
+ OUTPUT_SCRIPT deploy_script
+ NO_UNSUPPORTED_PLATFORM_ERROR
+)
+install(SCRIPT ${deploy_script})
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([&] {
diff --git a/examples/corelib/bindableproperties/doc/src/bindableproperties.qdoc b/examples/corelib/bindableproperties/doc/src/bindableproperties.qdoc
index 644693acb8..476522b086 100644
--- a/examples/corelib/bindableproperties/doc/src/bindableproperties.qdoc
+++ b/examples/corelib/bindableproperties/doc/src/bindableproperties.qdoc
@@ -3,7 +3,8 @@
/*!
\example bindableproperties
- \title Bindable Properties Example
+ \examplecategory {Data Processing & I/O}
+ \title Bindable Properties
\brief Demonstrates how the usage of bindable properties can simplify
your C++ code.
@@ -15,7 +16,7 @@
\image bindable_properties_example.png
- \section1 Modelling Subscription System with Signal/Slot Approach
+ \section1 Modeling Subscription System with Signal/Slot Approach
Let's first consider the usual pre-Qt 6 implementation.
To model the subscription service the \c Subscription class is used:
@@ -48,7 +49,7 @@
\note Both methods need to check if the data is actually changed and
only then emit the signals. \c setDuration() also needs to recalculate
- the price, when the duration has changed.
+ the price when the duration has changed.
The \c Subscription is not valid unless the user has a valid country and
age, so the validity is updated in the following way:
@@ -63,25 +64,25 @@
\snippet bindableproperties/subscription/user.cpp user-setters
In the \c main() function we initialize instances of \c User and
- \c Subsrcription:
+ \c Subscription:
\snippet bindableproperties/subscription/main.cpp init
- And do the proper signal-slot connections, to update the \c user and
- \c subsrcription data when UI elements change. That is straightforward,
+ And do the proper signal-slot connections to update the \c user and
+ \c subscription data when UI elements change. That is straightforward,
so we will skip this part.
- Next, we connect to \c Subscription::priceChanged(), to update the price
+ Next, we connect to \c Subscription::priceChanged() to update the price
in the UI when the price changes.
\snippet bindableproperties/subscription/main.cpp connect-price-changed
- We also connect to \c Subscription::isValidChanged(), to disable the price
+ We also connect to \c Subscription::isValidChanged() to disable the price
display if the subscription isn't valid.
\snippet bindableproperties/subscription/main.cpp connect-validity-changed
- Because the subsrcription price and validity also depend on the user's
+ Because the subscription price and validity also depend on the user's
country and age, we also need to connect to the \c User::countryChanged()
and \c User::ageChanged() signals and update \c subscription accordingly.
@@ -90,12 +91,12 @@
This works, but there are some problems:
\list
- \li There's a lot of boilerplate code for the signal-slot connections,
- to be able to react to changes to \c user or \c subscription. If any of
- the dependencies of the price changes, we need to remember to emit the
- corresponding notifier signals, to recalculate the price and update it in
+ \li There's a lot of boilerplate code for the signal-slot connections
+ in order to properly track changes to both \c user and \c subscription.
+ If any of the dependencies of the price changes, we need to remember to emit the
+ corresponding notifier signals, recalculate the price, and update it in
the UI.
- \li If more dependencies for price calculation are added in future, we'll
+ \li If more dependencies for price calculation are added in the future, we'll
need to add more signal-slot connections and make sure all the dependencies
are properly updated whenever any of them changes. The overall complexity
will grow, and the code will become harder to maintain.
@@ -109,7 +110,7 @@
Now let's see how the \l {Qt Bindable Properties} can help to solve the
same problem. First, let's have a look at the \c BindableSubscription class,
- which is similar to the \c Subscription class, but is implemented using the
+ which is similar to the \c Subscription class, but is implemented using
bindable properties:
\snippet bindableproperties/bindablesubscription/bindablesubscription.h bindable-subscription-class
@@ -156,7 +157,7 @@
changes the value. The subscriptions will stay alive as long as the
corresponding handlers are alive.
- Also note that the copy constructors of both \c BindableSubsrciption and
+ Also note that the copy constructors of both \c BindableSubscription and
\c BindableUser are disabled, since it's not defined what should happen
with their bindings when copying.
@@ -166,7 +167,7 @@
\list
\li The boilerplate code for the signal-slot connections is removed, the
dependencies are now tracked automatically.
- \li The code is easier to maintain. Adding more dependencies in future
+ \li The code is easier to maintain. Adding more dependencies in the future
will only require adding the corresponding bindable properties and setting
the binding expressions that reflect the relationships between each other.
\li The \c Subscription and \c User classes don't depend on the metaobject
diff --git a/examples/corelib/bindableproperties/shared/CMakeLists.txt b/examples/corelib/bindableproperties/shared/CMakeLists.txt
new file mode 100644
index 0000000000..efc85e5d4d
--- /dev/null
+++ b/examples/corelib/bindableproperties/shared/CMakeLists.txt
@@ -0,0 +1,23 @@
+# Copyright (C) 2023 The Qt Company Ltd.
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+add_library(bindableproperties_shared STATIC
+ subscriptionwindow.cpp
+ subscriptionwindow.h
+ subscriptionwindow.ui
+)
+
+target_link_libraries(bindableproperties_shared PUBLIC
+ Qt6::Core
+ Qt6::Gui
+ Qt6::Widgets
+)
+
+qt_add_resources(bindableproperties_shared "countries"
+ PREFIX
+ "/"
+ FILES
+ "finland.png"
+ "germany.png"
+ "norway.png"
+)
diff --git a/examples/corelib/bindableproperties/subscription/CMakeLists.txt b/examples/corelib/bindableproperties/subscription/CMakeLists.txt
index b4d5923b43..91b9340fbf 100644
--- a/examples/corelib/bindableproperties/subscription/CMakeLists.txt
+++ b/examples/corelib/bindableproperties/subscription/CMakeLists.txt
@@ -1,51 +1,12 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-cmake_minimum_required(VERSION 3.16)
-project(subscription 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/subscription")
-
-find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)
-
qt_add_executable(subscription
- ../shared/subscriptionwindow.cpp ../shared/subscriptionwindow.h ../shared/subscriptionwindow.ui
main.cpp
subscription.cpp subscription.h
user.cpp user.h
)
-target_link_libraries(subscription PUBLIC
- Qt::Core
- Qt::Gui
- Qt::Widgets
-)
-
-# Resources:
-set(countries_resource_files
- "../shared/finland.png"
- "../shared/germany.png"
- "../shared/norway.png"
-)
-
-qt_add_resources(subscription "countries"
- PREFIX
- "/"
- BASE
- "../shared"
- FILES
- ${countries_resource_files}
-)
-
-install(TARGETS subscription
- RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
- BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
- LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
+target_link_libraries(subscription PRIVATE
+ bindableproperties_shared
)
diff --git a/examples/corelib/bindableproperties/subscription/main.cpp b/examples/corelib/bindableproperties/subscription/main.cpp
index 8b3f37ab7c..3f98da7467 100644
--- a/examples/corelib/bindableproperties/subscription/main.cpp
+++ b/examples/corelib/bindableproperties/subscription/main.cpp
@@ -6,11 +6,14 @@
#include "user.h"
#include <QApplication>
-#include <QButtonGroup>
#include <QLabel>
+#include <QLocale>
#include <QPushButton>
#include <QRadioButton>
#include <QSpinBox>
+#include <QString>
+
+using namespace Qt::StringLiterals;
int main(int argc, char *argv[])
{
@@ -24,64 +27,65 @@ int main(int argc, char *argv[])
SubscriptionWindow w;
// Initialize subscription data
- QRadioButton *monthly = w.findChild<QRadioButton *>("btnMonthly");
+ QRadioButton *monthly = w.findChild<QRadioButton *>(u"btnMonthly"_s);
QObject::connect(monthly, &QRadioButton::clicked, &subscription, [&] {
subscription.setDuration(Subscription::Monthly);
});
- QRadioButton *quarterly = w.findChild<QRadioButton *>("btnQuarterly");
+ QRadioButton *quarterly = w.findChild<QRadioButton *>(u"btnQuarterly"_s);
QObject::connect(quarterly, &QRadioButton::clicked, &subscription, [&] {
subscription.setDuration(Subscription::Quarterly);
});
- QRadioButton *yearly = w.findChild<QRadioButton *>("btnYearly");
+ QRadioButton *yearly = w.findChild<QRadioButton *>(u"btnYearly"_s);
QObject::connect(yearly, &QRadioButton::clicked, &subscription, [&] {
subscription.setDuration(Subscription::Yearly);
});
// Initialize user data
- QPushButton *germany = w.findChild<QPushButton *>("btnGermany");
+ QPushButton *germany = w.findChild<QPushButton *>(u"btnGermany"_s);
QObject::connect(germany, &QPushButton::clicked, &user, [&] {
- user.setCountry(User::Germany);
+ user.setCountry(User::Country::Germany);
});
- QPushButton *finland = w.findChild<QPushButton *>("btnFinland");
+ QPushButton *finland = w.findChild<QPushButton *>(u"btnFinland"_s);
QObject::connect(finland, &QPushButton::clicked, &user, [&] {
- user.setCountry(User::Finland);
+ user.setCountry(User::Country::Finland);
});
- QPushButton *norway = w.findChild<QPushButton *>("btnNorway");
+ QPushButton *norway = w.findChild<QPushButton *>(u"btnNorway"_s);
QObject::connect(norway, &QPushButton::clicked, &user, [&] {
- user.setCountry(User::Norway);
+ user.setCountry(User::Country::Norway);
});
- QSpinBox *ageSpinBox = w.findChild<QSpinBox *>("ageSpinBox");
+ QSpinBox *ageSpinBox = w.findChild<QSpinBox *>(u"ageSpinBox"_s);
QObject::connect(ageSpinBox, &QSpinBox::valueChanged, &user, [&](int value) {
user.setAge(value);
});
// Initialize price data
- QLabel *priceDisplay = w.findChild<QLabel *>("priceDisplay");
+ QLabel *priceDisplay = w.findChild<QLabel *>(u"priceDisplay"_s);
priceDisplay->setText(QString::number(subscription.price()));
priceDisplay->setEnabled(subscription.isValid());
// Track the price changes
//! [connect-price-changed]
- QObject::connect(&subscription, &Subscription::priceChanged, [&] {
- priceDisplay->setText(QString::number(subscription.price()));
+ QObject::connect(&subscription, &Subscription::priceChanged, priceDisplay, [&] {
+ QLocale lc{QLocale::AnyLanguage, user.country()};
+ priceDisplay->setText(lc.toCurrencyString(subscription.price() / subscription.duration()));
});
//! [connect-price-changed]
//! [connect-validity-changed]
- QObject::connect(&subscription, &Subscription::isValidChanged, [&] {
+ QObject::connect(&subscription, &Subscription::isValidChanged, priceDisplay, [&] {
priceDisplay->setEnabled(subscription.isValid());
});
//! [connect-validity-changed]
//! [connect-user]
- QObject::connect(&user, &User::countryChanged, [&] {
+ QObject::connect(&user, &User::countryChanged, &subscription, [&] {
subscription.calculatePrice();
subscription.updateValidity();
});
- QObject::connect(&user, &User::ageChanged, [&] {
+ QObject::connect(&user, &User::ageChanged, &subscription, [&] {
subscription.updateValidity();
});
//! [connect-user]
diff --git a/examples/corelib/bindableproperties/subscription/subscription.cpp b/examples/corelib/bindableproperties/subscription/subscription.cpp
index 16bb6cfdd6..85cc5798cc 100644
--- a/examples/corelib/bindableproperties/subscription/subscription.cpp
+++ b/examples/corelib/bindableproperties/subscription/subscription.cpp
@@ -15,7 +15,7 @@ void Subscription::calculatePrice()
{
const auto oldPrice = m_price;
- m_price = qRound(calculateDiscount() * m_duration * basePrice());
+ m_price = qRound(calculateDiscount() * int(m_duration) * basePrice());
if (m_price != oldPrice)
emit priceChanged();
}
@@ -57,10 +57,10 @@ double Subscription::calculateDiscount() const
int Subscription::basePrice() const
{
- if (m_user->country() == User::None)
+ if (m_user->country() == User::Country::AnyTerritory)
return 0;
- return (m_user->country() == User::Norway) ? 100 : 80;
+ return (m_user->country() == User::Country::Norway) ? 100 : 80;
}
//! [calculate-base-price]
@@ -70,7 +70,7 @@ int Subscription::basePrice() const
void Subscription::updateValidity()
{
bool isValid = m_isValid;
- m_isValid = m_user->country() != User::None && m_user->age() > 12;
+ m_isValid = m_user->country() != User::Country::AnyTerritory && m_user->age() > 12;
if (m_isValid != isValid)
emit isValidChanged();
diff --git a/examples/corelib/bindableproperties/subscription/subscription.h b/examples/corelib/bindableproperties/subscription/subscription.h
index 5fa705772b..8f0d34e948 100644
--- a/examples/corelib/bindableproperties/subscription/subscription.h
+++ b/examples/corelib/bindableproperties/subscription/subscription.h
@@ -15,7 +15,7 @@ class Subscription : public QObject
{
Q_OBJECT
public:
- enum Duration { Monthly = 1, Quarterly = 4, Yearly = 12 };
+ enum Duration { Monthly = 1, Quarterly = 3, Yearly = 12 };
Subscription(User *user);
diff --git a/examples/corelib/bindableproperties/subscription/user.h b/examples/corelib/bindableproperties/subscription/user.h
index 1e16f9e901..dd32a4fe23 100644
--- a/examples/corelib/bindableproperties/subscription/user.h
+++ b/examples/corelib/bindableproperties/subscription/user.h
@@ -4,6 +4,7 @@
#ifndef USER_H
#define USER_H
+#include <QLocale>
#include <QObject>
//! [user-class]
@@ -13,13 +14,9 @@ class User : public QObject
Q_OBJECT
public:
- enum Country {
- None,
- Finland,
- Germany,
- Norway,
- };
+ using Country = QLocale::Territory;
+public:
Country country() const { return m_country; }
void setCountry(Country country);
@@ -31,8 +28,8 @@ signals:
void ageChanged();
private:
- Country m_country = Country::None;
- int m_age = 0;
+ Country m_country { QLocale::AnyTerritory };
+ int m_age { 0 };
};
//! [user-class]