本页内容

“Trivial Wizard”示例

“简单向导”示例演示了如何使用三个QWizardPage 实例和一个QWizard 实例来创建一个线性的三页注册向导。

从简介页面,到注册页面,再到结论页面的流程

简介页面

入门向导页面

介绍页面通过createIntroPage() 函数创建,该函数会创建一个QWizardPage 并将其标题设置为“介绍”。使用QLabel 来存储page 的描述内容。使用QVBoxLayout 来存储label 。当调用createIntroPage() 函数时,会返回该page 。

QWizardPage *createIntroPage()
{
    QWizardPage *page = new QWizardPage;
    page->setTitle("Introduction");

    QLabel *label = new QLabel("This wizard will help you register your copy "
                               "of Super Product Two.");
    label->setWordWrap(true);

    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(label);
    page->setLayout(layout);

    return page;
}

注册页面

注册向导页面

注册页面通过createRegistrationPage() 函数创建。使用QLineEdit 对象允许用户输入姓名和电子邮件地址。QGridLayout 用于保存QLabel 和QLineEdit 对象。

QWizardPage *createRegistrationPage()
{
    QWizardPage *page = new QWizardPage;
    page->setTitle("Registration");
    page->setSubTitle("Please fill both fields.");

    QLabel *nameLabel = new QLabel("Name:");
    QLineEdit *nameLineEdit = new QLineEdit;

    QLabel *emailLabel = new QLabel("Email address:");
    QLineEdit *emailLineEdit = new QLineEdit;

    QGridLayout *layout = new QGridLayout;
    layout->addWidget(nameLabel, 0, 0);
    layout->addWidget(nameLineEdit, 0, 1);
    layout->addWidget(emailLabel, 1, 0);
    layout->addWidget(emailLineEdit, 1, 1);
    page->setLayout(layout);

    return page;
}

结论页面

“完成向导”页面

结论页面是在createConclusionPage() 函数中创建的。该函数的内容与createIntroPage() 类似。使用QLabel 来告知用户注册过程已成功完成。

QWizardPage *createConclusionPage()
{
    QWizardPage *page = new QWizardPage;
    page->setTitle("Conclusion");

    QLabel *label = new QLabel("You are now successfully registered. Have a "
                               "nice day!");
    label->setWordWrap(true);

    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(label);
    page->setLayout(layout);

    return page;
}

main() 函数

main() 函数实例化了一个QWizard 对象wizard ,并将三个QWizardPage 对象全部添加到其中。将wizard 窗口标题设置为“Trivial Wizard”,并调用其show() 函数将其显示出来。

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

#if QT_CONFIG(translation)
    QTranslator translator;
    for (const QString &trPath : QLibraryInfo::paths(QLibraryInfo::TranslationsPath)) {
        if (translator.load(QLocale(), u"qtbase"_s, u"_"_s, trPath)) {
            app.installTranslator(&translator);
            break;
        }
    }
#endif

    QWizard wizard;
    wizard.addPage(createIntroPage());
    wizard.addPage(createRegistrationPage());
    wizard.addPage(createConclusionPage());

    wizard.setWindowTitle("Trivial Wizard");
    wizard.show();

    return app.exec();
}

示例项目 @ code.qt.io

另请参阅《 QWizard 》 和《License Wizard Example》。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.