mirror of
https://github.com/crystalidea/qt6windows7.git
synced 2025-07-03 15:55:27 +08:00
qt 6.5.1 original
This commit is contained in:
@ -0,0 +1,50 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(bindablesubscription LANGUAGES CXX)
|
||||
|
||||
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_standard_project_setup()
|
||||
|
||||
qt_add_executable(bindablesubscription
|
||||
../shared/subscriptionwindow.cpp ../shared/subscriptionwindow.h ../shared/subscriptionwindow.ui
|
||||
main.cpp
|
||||
bindablesubscription.cpp bindablesubscription.h
|
||||
bindableuser.cpp bindableuser.h
|
||||
)
|
||||
|
||||
target_link_libraries(bindablesubscription PRIVATE
|
||||
Qt6::Core
|
||||
Qt6::Gui
|
||||
Qt6::Widgets
|
||||
)
|
||||
|
||||
# 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}"
|
||||
)
|
@ -0,0 +1,51 @@
|
||||
// Copyright (C) 2021 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "bindablesubscription.h"
|
||||
#include "bindableuser.h"
|
||||
|
||||
//! [binding-expressions]
|
||||
|
||||
BindableSubscription::BindableSubscription(BindableUser *user) : m_user(user)
|
||||
{
|
||||
Q_ASSERT(user);
|
||||
|
||||
m_price.setBinding([this] { return qRound(calculateDiscount() * m_duration * basePrice()); });
|
||||
|
||||
m_isValid.setBinding([this] {
|
||||
return m_user->country() != BindableUser::Country::AnyCountry && m_user->age() > 12;
|
||||
});
|
||||
}
|
||||
|
||||
//! [binding-expressions]
|
||||
|
||||
//! [set-duration]
|
||||
|
||||
void BindableSubscription::setDuration(Duration newDuration)
|
||||
{
|
||||
m_duration = newDuration;
|
||||
}
|
||||
|
||||
//! [set-duration]
|
||||
|
||||
double BindableSubscription::calculateDiscount() const
|
||||
{
|
||||
switch (m_duration) {
|
||||
case Monthly:
|
||||
return 1;
|
||||
case Quarterly:
|
||||
return 0.9;
|
||||
case Yearly:
|
||||
return 0.6;
|
||||
}
|
||||
Q_ASSERT(false);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int BindableSubscription::basePrice() const
|
||||
{
|
||||
if (m_user->country() == BindableUser::Country::AnyCountry)
|
||||
return 0;
|
||||
|
||||
return (m_user->country() == BindableUser::Country::Norway) ? 100 : 80;
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
// 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 <QProperty>
|
||||
|
||||
class BindableUser;
|
||||
|
||||
//! [bindable-subscription-class]
|
||||
|
||||
class BindableSubscription
|
||||
{
|
||||
public:
|
||||
enum Duration { Monthly = 1, Quarterly = 3, Yearly = 12 };
|
||||
|
||||
BindableSubscription(BindableUser *user);
|
||||
BindableSubscription(const BindableSubscription &) = delete;
|
||||
|
||||
int price() const { return m_price; }
|
||||
QBindable<int> bindablePrice() { return &m_price; }
|
||||
|
||||
Duration duration() const { return m_duration; }
|
||||
void setDuration(Duration newDuration);
|
||||
QBindable<Duration> bindableDuration() { return &m_duration; }
|
||||
|
||||
bool isValid() const { return m_isValid; }
|
||||
QBindable<bool> bindableIsValid() { return &m_isValid; }
|
||||
|
||||
private:
|
||||
double calculateDiscount() const;
|
||||
int basePrice() const;
|
||||
|
||||
BindableUser *m_user;
|
||||
QProperty<Duration> m_duration { Monthly };
|
||||
QProperty<int> m_price { 0 };
|
||||
QProperty<bool> m_isValid { false };
|
||||
};
|
||||
|
||||
//! [bindable-subscription-class]
|
||||
|
||||
#endif // BNDABLESUBSCRIPTION_H
|
@ -0,0 +1,22 @@
|
||||
QT += widgets
|
||||
TARGET = bindablesubscription
|
||||
|
||||
SOURCES += main.cpp \
|
||||
bindablesubscription.cpp \
|
||||
bindableuser.cpp \
|
||||
../shared/subscriptionwindow.cpp
|
||||
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/corelib/bindableproperties/bindablesubscription
|
||||
INSTALLS += target
|
||||
|
||||
FORMS += \
|
||||
../shared/subscriptionwindow.ui
|
||||
|
||||
HEADERS += \
|
||||
bindablesubscription.h \
|
||||
bindableuser.h \
|
||||
../shared/subscriptionwindow.h
|
||||
|
||||
RESOURCES += \
|
||||
../shared/countries.qrc
|
||||
|
@ -0,0 +1,18 @@
|
||||
// Copyright (C) 2021 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "bindableuser.h"
|
||||
|
||||
//! [bindable-user-setters]
|
||||
|
||||
void BindableUser::setCountry(Country country)
|
||||
{
|
||||
m_country = country;
|
||||
}
|
||||
|
||||
void BindableUser::setAge(int age)
|
||||
{
|
||||
m_age = age;
|
||||
}
|
||||
|
||||
//! [bindable-user-setters]
|
@ -0,0 +1,36 @@
|
||||
// Copyright (C) 2021 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef BINDABLEUSER_H
|
||||
#define BINDABLEUSER_H
|
||||
|
||||
#include <QLocale>
|
||||
#include <QProperty>
|
||||
|
||||
//! [bindable-user-class]
|
||||
|
||||
class BindableUser
|
||||
{
|
||||
public:
|
||||
using Country = QLocale::Territory;
|
||||
|
||||
public:
|
||||
BindableUser() = default;
|
||||
BindableUser(const BindableUser &) = delete;
|
||||
|
||||
Country country() const { return m_country; }
|
||||
void setCountry(Country country);
|
||||
QBindable<Country> bindableCountry() { return &m_country; }
|
||||
|
||||
int age() const { return m_age; }
|
||||
void setAge(int age);
|
||||
QBindable<int> bindableAge() { return &m_age; }
|
||||
|
||||
private:
|
||||
QProperty<Country> m_country { QLocale::AnyTerritory };
|
||||
QProperty<int> m_age { 0 };
|
||||
};
|
||||
|
||||
//! [bindable-user-class]
|
||||
|
||||
#endif // BINDABLEUSER_H
|
@ -0,0 +1,72 @@
|
||||
// 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 <QLabel>
|
||||
#include <QPushButton>
|
||||
#include <QRadioButton>
|
||||
#include <QSpinBox>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
BindableUser user;
|
||||
BindableSubscription subscription(&user);
|
||||
|
||||
SubscriptionWindow w;
|
||||
|
||||
// Initialize subscription data
|
||||
QRadioButton *monthly = w.findChild<QRadioButton *>("btnMonthly");
|
||||
QObject::connect(monthly, &QRadioButton::clicked, [&] {
|
||||
subscription.setDuration(BindableSubscription::Monthly);
|
||||
});
|
||||
QRadioButton *quarterly = w.findChild<QRadioButton *>("btnQuarterly");
|
||||
QObject::connect(quarterly, &QRadioButton::clicked, [&] {
|
||||
subscription.setDuration(BindableSubscription::Quarterly);
|
||||
});
|
||||
QRadioButton *yearly = w.findChild<QRadioButton *>("btnYearly");
|
||||
QObject::connect(yearly, &QRadioButton::clicked, [&] {
|
||||
subscription.setDuration(BindableSubscription::Yearly);
|
||||
});
|
||||
|
||||
// Initialize user data
|
||||
QPushButton *germany = w.findChild<QPushButton *>("btnGermany");
|
||||
QObject::connect(germany, &QPushButton::clicked, [&] {
|
||||
user.setCountry(BindableUser::Country::Germany);
|
||||
});
|
||||
QPushButton *finland = w.findChild<QPushButton *>("btnFinland");
|
||||
QObject::connect(finland, &QPushButton::clicked, [&] {
|
||||
user.setCountry(BindableUser::Country::Finland);
|
||||
});
|
||||
QPushButton *norway = w.findChild<QPushButton *>("btnNorway");
|
||||
QObject::connect(norway, &QPushButton::clicked, [&] {
|
||||
user.setCountry(BindableUser::Country::Norway);
|
||||
});
|
||||
|
||||
QSpinBox *ageSpinBox = w.findChild<QSpinBox *>("ageSpinBox");
|
||||
QObject::connect(ageSpinBox, &QSpinBox::valueChanged, [&](int value) {
|
||||
user.setAge(value);
|
||||
});
|
||||
|
||||
QLabel *priceDisplay = w.findChild<QLabel *>("priceDisplay");
|
||||
|
||||
// Track price changes
|
||||
//! [update-ui]
|
||||
auto priceChangeHandler = subscription.bindablePrice().subscribe([&] {
|
||||
QLocale lc{QLocale::AnyLanguage, user.country()};
|
||||
priceDisplay->setText(lc.toCurrencyString(subscription.price() / subscription.duration()));
|
||||
});
|
||||
|
||||
auto priceValidHandler = subscription.bindableIsValid().subscribe([&] {
|
||||
priceDisplay->setEnabled(subscription.isValid());
|
||||
});
|
||||
//! [update-ui]
|
||||
|
||||
w.show();
|
||||
return a.exec();
|
||||
}
|
Reference in New Issue
Block a user