以下是代码。
.h
#ifndef LICENSEWIZARD_H
#define LICENSEWIZARD_H
#include
QT_BEGIN_NAMESPACE
class QCheckBox;
class QLabel;
class QLineEdit;
class QRadioButton;
QT_END_NAMESPACE
class LicenseWizard : public QWizard
{
Q_OBJECT
public:
enum { Page_Intro, Page_Evaluate, Page_Register, Page_Details,
Page_Conclusion };
LicenseWizard(QWidget *parent = 0);
private slots:
void showHelp();
};
class IntroPage : public QWizardPage
{
Q_OBJECT
public:
IntroPage(QWidget *parent = 0);
int nextId() const override;
private:
QLabel *topLabel;
QRadioButton *registerRadioButton;
QRadioButton *evaluateRadioButton;
};
class EvaluatePage : public QWizardPage
{
Q_OBJECT
public:
EvaluatePage(QWidget *parent = 0);
int nextId() const override;
private:
QLabel *nameLabel;
QLabel *emailLabel;
QLineEdit *nameLineEdit;
QLineEdit *emailLineEdit;
};
class RegisterPage : public QWizardPage
{
Q_OBJECT
public:
RegisterPage(QWidget *parent = 0);
int nextId() const override;
private:
QLabel *nameLabel;
QLabel *upgradeKeyLabel;
QLineEdit *nameLineEdit;
QLineEdit *upgradeKeyLineEdit;
};
class DetailsPage : public QWizardPage
{
Q_OBJECT
public:
DetailsPage(QWidget *parent = 0);
int nextId() const override;
private:
QLabel *companyLabel;
QLabel *emailLabel;
QLabel *postalLabel;
QLineEdit *companyLineEdit;
QLineEdit *emailLineEdit;
QLineEdit *postalLineEdit;
};
class ConclusionPage : public QWizardPage
{
Q_OBJECT
public:
ConclusionPage(QWidget *parent = 0);
void initializePage() override;
int nextId() const override;
void setVisible(bool visible) override;
private slots:
void printButtonClicked();
private:
QLabel *bottomLabel;
QCheckBox *agreeCheckBox;
};
#endif
.h
分析这是一个使用Qt框架实现的向导(Wizard)应用程序的头文件。这个应用程序利用QWizard类创建了一个包含多个页面的向导,以便用户能够有步骤地完成某些任务。这个应用程序包含以下几个类:
.cpp
#include
#if defined(QT_PRINTSUPPORT_LIB)
#include
#if QT_CONFIG(printdialog)
#include
#include
#endif
#endif
#include "licensewizard.h"
QString emailRegExp = QStringLiteral(".+@.+");
LicenseWizard::LicenseWizard(QWidget *parent)
: QWizard(parent)
{
setPage(Page_Intro, new IntroPage);
setPage(Page_Evaluate, new EvaluatePage);
setPage(Page_Register, new RegisterPage);
setPage(Page_Details, new DetailsPage);
setPage(Page_Conclusion, new ConclusionPage);
setStartId(Page_Intro);
#ifndef Q_OS_MAC
setWizardStyle(ModernStyle);
#endif
setOption(HaveHelpButton, true);
setPixmap(QWizard::LogoPixmap, QPixmap(":/images/logo.png"));
connect(this, &QWizard::helpRequested, this, &LicenseWizard::showHelp);
setWindowTitle(tr("License Wizard"));
}
void LicenseWizard::showHelp()
{
static QString lastHelpMessage;
QString message;
switch (currentId()) {
case Page_Intro:
message = tr("The decision you make here will affect which page you "
"get to see next.");
break;
case Page_Evaluate:
message = tr("Make sure to provide a valid email address, such as "
"[email protected].");
break;
case Page_Register:
message = tr("If you don't provide an upgrade key, you will be "
"asked to fill in your details.");
break;
case Page_Details:
message = tr("Make sure to provide a valid email address, such as "
"[email protected].");
break;
case Page_Conclusion:
message = tr("You must accept the terms and conditions of the "
"license to proceed.");
break;
default:
message = tr("This help is likely not to be of any help.");
}
if (lastHelpMessage == message)
message = tr("Sorry, I already gave what help I could. "
"Maybe you should try asking a human?");
QMessageBox::information(this, tr("License Wizard Help"), message);
lastHelpMessage = message;
}
IntroPage::IntroPage(QWidget *parent)
: QWizardPage(parent)
{
setTitle(tr("Introduction"));
setPixmap(QWizard::WatermarkPixmap, QPixmap(":/images/watermark.png"));
topLabel = new QLabel(tr("This wizard will help you register your copy of "
"Super Product One™ or start "
"evaluating the product."));
topLabel->setWordWrap(true);
registerRadioButton = new QRadioButton(tr("&Register your copy"));
evaluateRadioButton = new QRadioButton(tr("&Evaluate the product for 30 "
"days"));
registerRadioButton->setChecked(true);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(topLabel);
layout->addWidget(registerRadioButton);
layout->addWidget(evaluateRadioButton);
setLayout(layout);
}
int IntroPage::nextId() const
{
if (evaluateRadioButton->isChecked()) {
return LicenseWizard::Page_Evaluate;
} else {
return LicenseWizard::Page_Register;
}
}
EvaluatePage::EvaluatePage(QWidget *parent)
: QWizardPage(parent)
{
setTitle(tr("Evaluate Super Product One™"));
setSubTitle(tr("Please fill both fields. Make sure to provide a valid "
"email address (e.g., [email protected])."));
nameLabel = new QLabel(tr("N&ame:"));
nameLineEdit = new QLineEdit;
nameLabel->setBuddy(nameLineEdit);
emailLabel = new QLabel(tr("&Email address:"));
emailLineEdit = new QLineEdit;
emailLineEdit->setValidator(new QRegularExpressionValidator(QRegularExpression(emailRegExp), this));
emailLabel->setBuddy(emailLineEdit);
registerField("evaluate.name*", nameLineEdit);
registerField("evaluate.email*", emailLineEdit);
QGridLayout *layout = new QGridLayout;
layout->addWidget(nameLabel, 0, 0);
layout->addWidget(nameLineEdit, 0, 1);
layout->addWidget(emailLabel, 1, 0);
layout->addWidget(emailLineEdit, 1, 1);
setLayout(layout);
}
int EvaluatePage::nextId() const
{
return LicenseWizard::Page_Conclusion;
}
RegisterPage::RegisterPage(QWidget *parent)
: QWizardPage(parent)
{
setTitle(tr("Register Your Copy of Super Product One™"));
setSubTitle(tr("If you have an upgrade key, please fill in "
"the appropriate field."));
nameLabel = new QLabel(tr("N&ame:"));
nameLineEdit = new QLineEdit;
nameLabel->setBuddy(nameLineEdit);
upgradeKeyLabel = new QLabel(tr("&Upgrade key:"));
upgradeKeyLineEdit = new QLineEdit;
upgradeKeyLabel->setBuddy(upgradeKeyLineEdit);
registerField("register.name*", nameLineEdit);
registerField("register.upgradeKey", upgradeKeyLineEdit);
QGridLayout *layout = new QGridLayout;
layout->addWidget(nameLabel, 0, 0);
layout->addWidget(nameLineEdit, 0, 1);
layout->addWidget(upgradeKeyLabel, 1, 0);
layout->addWidget(upgradeKeyLineEdit, 1, 1);
setLayout(layout);
}
int RegisterPage::nextId() const
{
if (upgradeKeyLineEdit->text().isEmpty()) {
return LicenseWizard::Page_Details;
} else {
return LicenseWizard::Page_Conclusion;
}
}
DetailsPage::DetailsPage(QWidget *parent)
: QWizardPage(parent)
{
setTitle(tr("Fill In Your Details"));
setSubTitle(tr("Please fill all three fields. Make sure to provide a valid "
"email address (e.g., [email protected])."));
companyLabel = new QLabel(tr("&Company name:"));
companyLineEdit = new QLineEdit;
companyLabel->setBuddy(companyLineEdit);
emailLabel = new QLabel(tr("&Email address:"));
emailLineEdit = new QLineEdit;
emailLineEdit->setValidator(new QRegularExpressionValidator(QRegularExpression(emailRegExp), this));
emailLabel->setBuddy(emailLineEdit);
postalLabel = new QLabel(tr("&Postal address:"));
postalLineEdit = new QLineEdit;
postalLabel->setBuddy(postalLineEdit);
registerField("details.company*", companyLineEdit);
registerField("details.email*", emailLineEdit);
registerField("details.postal*", postalLineEdit);
QGridLayout *layout = new QGridLayout;
layout->addWidget(companyLabel, 0, 0);
layout->addWidget(companyLineEdit, 0, 1);
layout->addWidget(emailLabel, 1, 0);
layout->addWidget(emailLineEdit, 1, 1);
layout->addWidget(postalLabel, 2, 0);
layout->addWidget(postalLineEdit, 2, 1);
setLayout(layout);
}
int DetailsPage::nextId() const
{
return LicenseWizard::Page_Conclusion;
}
ConclusionPage::ConclusionPage(QWidget *parent)
: QWizardPage(parent)
{
setTitle(tr("Complete Your Registration"));
setPixmap(QWizard::WatermarkPixmap, QPixmap(":/images/watermark.png"));
bottomLabel = new QLabel;
bottomLabel->setWordWrap(true);
agreeCheckBox = new QCheckBox(tr("I agree to the terms of the license"));
registerField("conclusion.agree*", agreeCheckBox);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(bottomLabel);
layout->addWidget(agreeCheckBox);
setLayout(layout);
}
int ConclusionPage::nextId() const
{
return -1;
}
void ConclusionPage::initializePage()
{
QString licenseText;
if (wizard()->hasVisitedPage(LicenseWizard::Page_Evaluate)) {
licenseText = tr("Evaluation License Agreement: "
"You can use this software for 30 days and make one "
"backup, but you are not allowed to distribute it.");
} else if (wizard()->hasVisitedPage(LicenseWizard::Page_Details)) {
licenseText = tr("First-Time License Agreement: "
"You can use this software subject to the license "
"you will receive by email.");
} else {
licenseText = tr("Upgrade License Agreement: "
"This software is licensed under the terms of your "
"current license.");
}
bottomLabel->setText(licenseText);
}
void ConclusionPage::setVisible(bool visible)
{
QWizardPage::setVisible(visible);
if (visible) {
wizard()->setButtonText(QWizard::CustomButton1, tr("&Print"));
wizard()->setOption(QWizard::HaveCustomButton1, true);
connect(wizard(), &QWizard::customButtonClicked,
this, &ConclusionPage::printButtonClicked);
} else {
wizard()->setOption(QWizard::HaveCustomButton1, false);
disconnect(wizard(), &QWizard::customButtonClicked,
this, &ConclusionPage::printButtonClicked);
}
}
void ConclusionPage::printButtonClicked()
{
#if QT_CONFIG(printdialog)
QPrinter printer;
QPrintDialog dialog(&printer, this);
if (dialog.exec())
QMessageBox::warning(this, tr("Print License"),
tr("As an environmentally friendly measure, the "
"license text will not actually be printed."));
#endif
}
.cpp
分析cpp用于实现
.main
#include
#include
#include
#include
#include "licensewizard.h"
int main(int argc, char *argv[])
{
Q_INIT_RESOURCE(licensewizard);
QApplication app(argc, argv);
#ifndef QT_NO_TRANSLATION
QString translatorFileName = QLatin1String("qt_");
translatorFileName += QLocale::system().name();
QTranslator *translator = new QTranslator(&app);
if (translator->load(translatorFileName, QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
app.installTranslator(translator);
#endif
LicenseWizard wizard;
wizard.show();
return app.exec();
}
.main
分析#ifndef QT_NO_TRANSLATION
QString translatorFileName = QLatin1String("qt_");
translatorFileName += QLocale::system().name();
QTranslator *translator = new QTranslator(&app);
if (translator->load(translatorFileName, QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
app.installTranslator(translator);
#endif
这段代码是一个条件编译指令,用于根据Qt框架是否已启用本地化支持来加载Qt翻译文件,以实现多语言支持。具体实现如下:
其中,QLatin1String是一个Qt框架提供的用于创建一个不可修改的QString对象的宏。此外,QLocale::system().name()用于获取当前系统的语言名称。最后,QLibraryInfo::location(QLibraryInfo::TranslationsPath)用于获取Qt框架的翻译文件路径。