【Qt之QWizard】使用2,示例分析

效果图

在这里插入图片描述
根据首页的选择不同,进入不同的选项。

以下是代码。

示例

.h
#ifndef LICENSEWIZARD_H
#define LICENSEWIZARD_H#include <QWizard>QT_BEGIN_NAMESPACE
class QCheckBox;
class QLabel;
class QLineEdit;
class QRadioButton;
QT_END_NAMESPACEclass LicenseWizard : public QWizard
{Q_OBJECTpublic:enum { Page_Intro, Page_Evaluate, Page_Register, Page_Details,Page_Conclusion };LicenseWizard(QWidget *parent = 0);private slots:void showHelp();
};class IntroPage : public QWizardPage
{Q_OBJECTpublic:IntroPage(QWidget *parent = 0);int nextId() const override;private:QLabel *topLabel;QRadioButton *registerRadioButton;QRadioButton *evaluateRadioButton;
};class EvaluatePage : public QWizardPage
{Q_OBJECTpublic:EvaluatePage(QWidget *parent = 0);int nextId() const override;private:QLabel *nameLabel;QLabel *emailLabel;QLineEdit *nameLineEdit;QLineEdit *emailLineEdit;
};class RegisterPage : public QWizardPage
{Q_OBJECTpublic:RegisterPage(QWidget *parent = 0);int nextId() const override;private:QLabel *nameLabel;QLabel *upgradeKeyLabel;QLineEdit *nameLineEdit;QLineEdit *upgradeKeyLineEdit;
};class DetailsPage : public QWizardPage
{Q_OBJECTpublic: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_OBJECTpublic: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类创建了一个包含多个页面的向导,以便用户能够有步骤地完成某些任务。这个应用程序包含以下几个类:

  • LicenseWizard:一个继承自QWizard的类,表示整个向导应用程序。它包含了多个页面,如IntroPage、EvaluatePage、RegisterPage、DetailsPage和ConclusionPage。
  • IntroPage:向导的第一页,包含一个标题、两个单选钮(一个表示注册,另一个表示试用)和一个“帮助”按钮。
  • EvaluatePage:向导的第二页,用于试用授权。包含两个标签和两个文本框(用于输入用户姓名和电子邮件)。
  • RegisterPage:向导的第三页,用于注册授权。包含两个标签和两个文本框(用于输入用户姓名和注册密钥)。
  • DetailsPage:向导的第四页,用于输入用户详细信息。包含三个标签和三个文本框(用于输入公司名称、电子邮件和邮政编码)。
  • ConclusionPage:向导的最后一页,显示一条消息和一个复选框(用于表示用户同意某些条款)。包含一个“打印”按钮以及相关的槽函数,用于处理用户单击该按钮的事件。
.cpp
#include <QtWidgets>
#if defined(QT_PRINTSUPPORT_LIB)
#include <QtPrintSupport/qtprintsupportglobal.h>
#if QT_CONFIG(printdialog)
#include <QPrinter>
#include <QPrintDialog>
#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_MACsetWizardStyle(ModernStyle);
#endifsetOption(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 ""toni.buddenbrook@example.de.");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 ""thomas.gradgrind@example.co.uk.");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 ""<i>Super Product One</i>&trade; 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 <i>Super Product One</i>&trade;"));setSubTitle(tr("Please fill both fields. Make sure to provide a valid ""email address (e.g., john.smith@example.com)."));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 <i>Super Product One</i>&trade;"));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., tanaka.aya@example.co.jp)."));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("<u>Evaluation License Agreement:</u> ""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("<u>First-Time License Agreement:</u> ""You can use this software subject to the license ""you will receive by email.");} else {licenseText = tr("<u>Upgrade License Agreement:</u> ""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 <QApplication>
#include <QTranslator>
#include <QLocale>
#include <QLibraryInfo>#include "licensewizard.h"int main(int argc, char *argv[])
{Q_INIT_RESOURCE(licensewizard);QApplication app(argc, argv);#ifndef QT_NO_TRANSLATIONQString translatorFileName = QLatin1String("qt_");translatorFileName += QLocale::system().name();QTranslator *translator = new QTranslator(&app);if (translator->load(translatorFileName, QLibraryInfo::location(QLibraryInfo::TranslationsPath)))app.installTranslator(translator);
#endifLicenseWizard wizard;wizard.show();return app.exec();
}
.main分析
#ifndef QT_NO_TRANSLATIONQString 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翻译文件,以实现多语言支持。具体实现如下:

  • 如果Qt框架没有启用本地化支持,则直接跳过这段代码,不做任何操作。
  • 如果Qt框架已启用本地化支持,则根据当前系统语言名称生成对应的Qt翻译文件名,并尝试加载该文件。加载成功后,将翻译文件安装到应用程序中,从而使应用程序在当前语言环境下显示正确的翻译文本。

其中,QLatin1String是一个Qt框架提供的用于创建一个不可修改的QString对象的宏。此外,QLocale::system().name()用于获取当前系统的语言名称。最后,QLibraryInfo::location(QLibraryInfo::TranslationsPath)用于获取Qt框架的翻译文件路径。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/175482.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java实现计数排序

图解 计数排序是一种线性时间复杂度的排序算法&#xff0c;它不基于比较排序&#xff0c;而是根据待排序序列中元素的值来进行排序。 具体的过程如下&#xff1a; 统计序列中每个元素出现的个数&#xff0c;得到一个计数数组count。其中&#xff0c;count[i]表示待排序序列中值…

npm封装插件打包上传后图片资源错误

问题&#xff1a; npm封装插件&#xff1a;封装的组件页面涉及使用图片资源&#xff0c;在封装的项目里调用图片显示正常&#xff1b;但是打包上传后&#xff0c;其他项目引入使用报错找不到图片资源&#xff1b;图片路径也不对 获取图片的base64方法 解决方案&#xff1a; 将…

Django debug page XSS漏洞复现_(CVE-2017-12794)

Django debug page XSS漏洞复现_(CVE-2017-12794) 复现过程 首先进入靶场环境 按照他写的&#xff0c;需要给这个变量赋值 创建一个用户&#xff0c;用弹窗做用户名 http://10.4.7.137:8000/create_user/?username<script>alert(1)</script>返回&#xff0c;然…

时间序列基础->数据标签、数据分割器、数据加载器的定义和讲解(零基础入门时间序列)

一、本文介绍 各位小伙伴好&#xff0c;最近在发时间序列的实战案例中总是有一些朋友问我时间序列中的部分对数据的操作是什么含义&#xff0c;我进行了挺多的介绍和讲解但是问的人越来越多&#xff0c;所以今天在这里单独发一篇文章来单独的讲一下时间序列中对数据的处理操作…

消息队列简介

什么是消息队列?&#xff08;Message queue&#xff0c;简称MQ&#xff09; 从字面理解就是一个保存消息的一个容器。那么我们为何需要这样一个容器呢&#xff1f; 其实就是为了解耦各个系统&#xff0c;我们来举个例子&#xff1a; 有这么一个简单的场景&#xff0c;系统A负…

机器学习—基本术语

目录 1.样本&#xff08;示例&#xff09; 2.属性 3.属性值 4.属性空间 5.样本空间 6.学习&#xff08;训练&#xff09; 7.数据集 8.测试 9.假设 10.学习器 11.标记 12.样例 13.标记空间&#xff08;样例空间&#xff09; 14.分类与回归 15.有监督学习、无监督…

msvcp140.dll丢失的解决方法、详细解析dll缺失原因及对电脑的影响

msvcp140.dll是一款Visual C Redistributable for Visual Studio 2015的运行时库&#xff0c;许多程序都需要依赖这个库才能正常运行。当msvcp140.dll丢失时&#xff0c;我们可能会遇到无法打开程序或游戏&#xff0c;甚至系统崩溃的问题。本文将详细介绍msvcp140.dll丢失的解决…

【Liunx】部署WEB服务:Apache

【Liunx】部署WEB服务:Apache 概述Apache1.介绍2.Apache文件路径3.Apache详解(1)安装Apache(2)启动Apache(3)配置文件a.Apache主配置文件&#xff1a;vim /etc/httpd/conf/httpd.conf信息&#xff1a;b.基于主机头的虚拟主机 (4)开始演示&#xff1a;a.新建两个网站根目录b.分别…

【MediaFoundation】相关的概念

MF 概览 Media Foundation 提供了两种不同的编程模型&#xff0c;左边展示的是端到端的媒体数据模型&#xff0c;主要用在&#xff1a;播放URL或者文件&#xff0c;以及控制流。 在图表右侧展示的第二种模型中&#xff0c;应用程序可以从源头拉取数据&#xff0c;也可以将数据…

目标检测算法 - YOLOv2

文章目录 1. Batch Normalization2. High Resolution Classifier3. Anchor、Dimension Cluster、Direct location prediction4. Loss Function5. Fine-Grained Features6. Multi-Scale Training7. Faster8. Stronger Better&#xff0c;Faster&#xff0c;Stronger。 2017年&am…

Java实现DXF文件转换成PDF

代码实现 public static void dxfToPdf(){// 加载DXF文件String inputFile "input.dxf";CadImage cadImage (CadImage) Image.load(inputFile);// 设置PDF输出选项PdfOptions pdfOptions new PdfOptions();pdfOptions.setPageWidth(200);pdfOptions.setPageHeigh…