diff options
Diffstat (limited to 'examples/corelib/bindableproperties')
17 files changed, 185 insertions, 782 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 6332c45e1a..2734f44a17 100644 --- a/examples/corelib/bindableproperties/bindablesubscription/CMakeLists.txt +++ b/examples/corelib/bindableproperties/bindablesubscription/CMakeLists.txt @@ -1,48 +1,15 @@ -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) +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause 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 14b28b3fae..32f4194635 100644 --- a/examples/corelib/bindableproperties/bindablesubscription/bindablesubscription.cpp +++ b/examples/corelib/bindableproperties/bindablesubscription/bindablesubscription.cpp @@ -1,52 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #include "bindablesubscription.h" #include "bindableuser.h" @@ -57,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; }); } @@ -85,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 763864d627..03870d0617 100644 --- a/examples/corelib/bindableproperties/bindablesubscription/bindablesubscription.h +++ b/examples/corelib/bindableproperties/bindablesubscription/bindablesubscription.h @@ -1,57 +1,10 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #ifndef BINDABLESUBSCRIPTION_H #define BINDABLESUBSCRIPTION_H -#include <QPointer> +#include <QBindable> #include <QProperty> class BindableUser; @@ -61,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.cpp b/examples/corelib/bindableproperties/bindablesubscription/bindableuser.cpp index d30e203b61..9cc3b7a4a6 100644 --- a/examples/corelib/bindableproperties/bindablesubscription/bindableuser.cpp +++ b/examples/corelib/bindableproperties/bindablesubscription/bindableuser.cpp @@ -1,52 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #include "bindableuser.h" diff --git a/examples/corelib/bindableproperties/bindablesubscription/bindableuser.h b/examples/corelib/bindableproperties/bindablesubscription/bindableuser.h index 65541e6ac1..6bb9bcdcb5 100644 --- a/examples/corelib/bindableproperties/bindablesubscription/bindableuser.h +++ b/examples/corelib/bindableproperties/bindablesubscription/bindableuser.h @@ -1,56 +1,11 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #ifndef BINDABLEUSER_H #define BINDABLEUSER_H +#include <QBindable> +#include <QLocale> #include <QProperty> //! [bindable-user-class] @@ -58,13 +13,9 @@ class BindableUser { public: - enum Country { - None, - Finland, - Germany, - Norway, - }; + using Country = QLocale::Territory; +public: BindableUser() = default; BindableUser(const BindableUser &) = delete; @@ -77,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 df935679ad..466f487b8e 100644 --- a/examples/corelib/bindableproperties/bindablesubscription/main.cpp +++ b/examples/corelib/bindableproperties/bindablesubscription/main.cpp @@ -1,63 +1,20 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #include "../shared/subscriptionwindow.h" #include "bindablesubscription.h" #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[]) { @@ -66,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 8c36921ff8..476522b086 100644 --- a/examples/corelib/bindableproperties/doc/src/bindableproperties.qdoc +++ b/examples/corelib/bindableproperties/doc/src/bindableproperties.qdoc @@ -1,33 +1,10 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the documentation of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:FDL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Free Documentation License Usage -** Alternatively, this file may be used under the terms of the GNU Free -** Documentation License version 1.3 as published by the Free Software -** Foundation and appearing in the file included in the packaging of -** this file. Please review the following information to ensure -** the GNU Free Documentation License version 1.3 requirements -** will be met: https://www.gnu.org/licenses/fdl-1.3.html. -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only /*! \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. @@ -39,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: @@ -72,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: @@ -87,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. @@ -114,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. @@ -133,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 @@ -149,7 +126,7 @@ \snippet bindableproperties/bindablesubscription/bindableuser.h bindable-user-class - The second differenece is in the implementation of these calsses. First of + The second difference is in the implementation of these classes. First of all, the dependencies between \c subscription and \c user are now tracked via binding expressions: @@ -180,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. @@ -190,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/shared/subscriptionwindow.cpp b/examples/corelib/bindableproperties/shared/subscriptionwindow.cpp index 03269dfa0c..0e17283d40 100644 --- a/examples/corelib/bindableproperties/shared/subscriptionwindow.cpp +++ b/examples/corelib/bindableproperties/shared/subscriptionwindow.cpp @@ -1,52 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #include "subscriptionwindow.h" #include "ui_subscriptionwindow.h" diff --git a/examples/corelib/bindableproperties/shared/subscriptionwindow.h b/examples/corelib/bindableproperties/shared/subscriptionwindow.h index 9bc3acd91a..75f6a1eb83 100644 --- a/examples/corelib/bindableproperties/shared/subscriptionwindow.h +++ b/examples/corelib/bindableproperties/shared/subscriptionwindow.h @@ -1,52 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #ifndef SUBSCRIPTIONWINDOW_H #define SUBSCRIPTIONWINDOW_H diff --git a/examples/corelib/bindableproperties/subscription/CMakeLists.txt b/examples/corelib/bindableproperties/subscription/CMakeLists.txt index 9b624e4e29..91b9340fbf 100644 --- a/examples/corelib/bindableproperties/subscription/CMakeLists.txt +++ b/examples/corelib/bindableproperties/subscription/CMakeLists.txt @@ -1,48 +1,12 @@ -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) +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause 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 6ff0c960f1..3f98da7467 100644 --- a/examples/corelib/bindableproperties/subscription/main.cpp +++ b/examples/corelib/bindableproperties/subscription/main.cpp @@ -1,63 +1,19 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #include "../shared/subscriptionwindow.h" #include "subscription.h" #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[]) { @@ -71,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 20efe825f3..85cc5798cc 100644 --- a/examples/corelib/bindableproperties/subscription/subscription.cpp +++ b/examples/corelib/bindableproperties/subscription/subscription.cpp @@ -1,52 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #include "subscription.h" #include "user.h" @@ -62,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(); } @@ -104,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] @@ -117,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 41d052e86f..8f0d34e948 100644 --- a/examples/corelib/bindableproperties/subscription/subscription.h +++ b/examples/corelib/bindableproperties/subscription/subscription.h @@ -1,52 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #ifndef SUBSCRIPTION_H #define SUBSCRIPTION_H @@ -62,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.cpp b/examples/corelib/bindableproperties/subscription/user.cpp index c2b0835330..575bcb13ee 100644 --- a/examples/corelib/bindableproperties/subscription/user.cpp +++ b/examples/corelib/bindableproperties/subscription/user.cpp @@ -1,52 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #include "user.h" diff --git a/examples/corelib/bindableproperties/subscription/user.h b/examples/corelib/bindableproperties/subscription/user.h index 8c9ebb1009..dd32a4fe23 100644 --- a/examples/corelib/bindableproperties/subscription/user.h +++ b/examples/corelib/bindableproperties/subscription/user.h @@ -1,56 +1,10 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #ifndef USER_H #define USER_H +#include <QLocale> #include <QObject> //! [user-class] @@ -60,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); @@ -78,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] |