Nesta página

Exemplo do Trivial Wizard

O exemplo do Assistente Trivial ilustra como criar um assistente de registro linear de três páginas usando três instâncias de QWizardPage e uma instância de QWizard.

Fluxo desde a introdução, passando pelo cadastro, até a página de conclusão

Página de introdução

Página do assistente de introdução

A página de introdução é criada com a função ` createIntroPage() `, na qual um ` QWizardPage ` é criado e seu título é definido como “Introdução”. Um ` QLabel ` é usado para conter a descrição de ` page`. Um ` QVBoxLayout ` é usado para conter o ` label`. Esse ` page ` é retornado quando a função ` createIntroPage() ` é chamada.

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;
}

Página de cadastro

Página do assistente de cadastro

A página de cadastro é criada com a função createRegistrationPage(). Objetos QLineEdit são usados para permitir que o usuário insira um nome e um endereço de e-mail. Um QGridLayout é usado para armazenar os objetos QLabel e 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;
}

Página de conclusão

Página do assistente de conclusão

A página de conclusão é criada na função createConclusionPage(). O conteúdo dessa função é semelhante ao de createIntroPage(). Um objeto QLabel é usado para informar ao usuário que o processo de cadastro foi concluído com sucesso.

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() Função

A função main() instancia um objeto QWizard, wizard, e adiciona a ele todos os três objetos QWizardPage. O título da janela wizard é definido como “Trivial Wizard” e sua função show() é chamada para exibi-la.

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();
}

Projeto de exemplo @ code.qt.io

Veja também QWizard e o Exemplo do Assistente de Licença.

© 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.