Revamp Subscription example: Cretae QString using a Literal operator

Create QString at compile time using the literal operator""s instead of
using pointer to characters. This way, no conversion or allocation will
occur at runtime.

Task-number: QTBUG-117425
Pick-to: 6.6 6.5
Change-Id: I2a19d4fe1150aad7908d0c2a7215099bc0814e8d
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
This commit is contained in:
Rym Bouabid 2023-09-21 16:50:08 +02:00
parent 9ed067e719
commit ba98644180

View File

@ -13,6 +13,8 @@
#include <QSpinBox> #include <QSpinBox>
#include <QString> #include <QString>
using namespace Qt::StringLiterals;
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QApplication a(argc, argv); QApplication a(argc, argv);
@ -25,40 +27,40 @@ int main(int argc, char *argv[])
SubscriptionWindow w; SubscriptionWindow w;
// Initialize subscription data // Initialize subscription data
QRadioButton *monthly = w.findChild<QRadioButton *>("btnMonthly"); QRadioButton *monthly = w.findChild<QRadioButton *>(u"btnMonthly"_s);
QObject::connect(monthly, &QRadioButton::clicked, &subscription, [&] { QObject::connect(monthly, &QRadioButton::clicked, &subscription, [&] {
subscription.setDuration(Subscription::Monthly); subscription.setDuration(Subscription::Monthly);
}); });
QRadioButton *quarterly = w.findChild<QRadioButton *>("btnQuarterly"); QRadioButton *quarterly = w.findChild<QRadioButton *>(u"btnQuarterly"_s);
QObject::connect(quarterly, &QRadioButton::clicked, &subscription, [&] { QObject::connect(quarterly, &QRadioButton::clicked, &subscription, [&] {
subscription.setDuration(Subscription::Quarterly); subscription.setDuration(Subscription::Quarterly);
}); });
QRadioButton *yearly = w.findChild<QRadioButton *>("btnYearly"); QRadioButton *yearly = w.findChild<QRadioButton *>(u"btnYearly"_s);
QObject::connect(yearly, &QRadioButton::clicked, &subscription, [&] { QObject::connect(yearly, &QRadioButton::clicked, &subscription, [&] {
subscription.setDuration(Subscription::Yearly); subscription.setDuration(Subscription::Yearly);
}); });
// Initialize user data // Initialize user data
QPushButton *germany = w.findChild<QPushButton *>("btnGermany"); QPushButton *germany = w.findChild<QPushButton *>(u"btnGermany"_s);
QObject::connect(germany, &QPushButton::clicked, &user, [&] { QObject::connect(germany, &QPushButton::clicked, &user, [&] {
user.setCountry(User::Country::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, [&] { QObject::connect(finland, &QPushButton::clicked, &user, [&] {
user.setCountry(User::Country::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, [&] { QObject::connect(norway, &QPushButton::clicked, &user, [&] {
user.setCountry(User::Country::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) { QObject::connect(ageSpinBox, &QSpinBox::valueChanged, &user, [&](int value) {
user.setAge(value); user.setAge(value);
}); });
// Initialize price data // Initialize price data
QLabel *priceDisplay = w.findChild<QLabel *>("priceDisplay"); QLabel *priceDisplay = w.findChild<QLabel *>(u"priceDisplay"_s);
priceDisplay->setText(QString::number(subscription.price())); priceDisplay->setText(QString::number(subscription.price()));
priceDisplay->setEnabled(subscription.isValid()); priceDisplay->setEnabled(subscription.isValid());