Qt小例子学习84 - QWizard

Qt小例子学习84 - QWizard

Wizard.h

#ifndef WIZARD_H
#define WIZARD_H

#include <QWizard>

class QButtonGroup;

class Wizard : public QWizard
{
    Q_OBJECT
public:
    enum { Start_Page, Page_1, Page_2, Page_3, Page_4, End_Page };
    Wizard(QWidget *parent = nullptr);
};

class StartPage : public QWizardPage
{
    Q_OBJECT
public:
    StartPage(QWidget *parent = nullptr);
    int nextId() const override;

private:
    QButtonGroup *group;
};

class Page1 : public QWizardPage
{
    Q_OBJECT
public:
    Page1(QWidget *parent = nullptr);
    int nextId() const override;
};

class Page2 : public QWizardPage
{
    Q_OBJECT
public:
    Page2(QWidget *parent = nullptr);
    int nextId() const override;
};

class Page3 : public QWizardPage
{
    Q_OBJECT
public:
    Page3(QWidget *parent = nullptr);
    int nextId() const override;
};

class Page4 : public QWizardPage
{
    Q_OBJECT
public:
    Page4(QWidget *parent = nullptr);
    int nextId() const override;
};

class EndPage : public QWizardPage
{
    Q_OBJECT
public:
    EndPage(QWidget *parent = nullptr);
    int nextId() const override;
};
#endif // WIZARD_H

Wizard.cpp

#include "wizard.h"

#include <QButtonGroup>
#include <QLabel>
#include <QRadioButton>
#include <QVBoxLayout>

Wizard::Wizard(QWidget *parent) : QWizard(parent)
{
    setPage(Start_Page, new StartPage);
    setPage(Page_1, new Page1);
    setPage(Page_2, new Page3);
    setPage(Page_3, new Page3);
    setPage(Page_4, new Page4);
    setPage(End_Page, new EndPage);
}

StartPage::StartPage(QWidget *parent) : QWizardPage(parent)
{
    QVBoxLayout *lay = new QVBoxLayout(this);
    lay->addWidget(new QLabel("Start Page"));
    group = new QButtonGroup(this);
    for (const QString &text : {"Page1", "Page2", "Page4"})
    {
        QRadioButton *radio = new QRadioButton(text);
        lay->addWidget(radio);
        group->addButton(radio);
    }
    group->buttons().first()->setChecked(true);
    lay->addStretch();
}

int StartPage::nextId() const
{
    if (group->checkedButton()->text() == "Page1")
        return Wizard::Page_1;
    else if (group->checkedButton()->text() == "Page2")
        return Wizard::Page_2;
    return Wizard::Page_4;
}

Page1::Page1(QWidget *parent) : QWizardPage(parent)
{
    QVBoxLayout *lay = new QVBoxLayout(this);
    lay->addWidget(new QLabel("Page1"));
}

int Page1::nextId() const { return Wizard::End_Page; }

Page2::Page2(QWidget *parent) : QWizardPage(parent)
{
    QVBoxLayout *lay = new QVBoxLayout(this);
    lay->addWidget(new QLabel("Page2"));
}

int Page2::nextId() const { return Wizard::Page_3; }

Page3::Page3(QWidget *parent) : QWizardPage(parent)
{
    QVBoxLayout *lay = new QVBoxLayout(this);
    lay->addWidget(new QLabel("Page3"));
}

int Page3::nextId() const { return Wizard::End_Page; }

Page4::Page4(QWidget *parent) : QWizardPage(parent)
{
    QVBoxLayout *lay = new QVBoxLayout(this);
    lay->addWidget(new QLabel("Page4"));
}

int Page4::nextId() const { return Wizard::End_Page; }

EndPage::EndPage(QWidget *parent) : QWizardPage(parent)
{
    QVBoxLayout *lay = new QVBoxLayout(this);
    lay->addWidget(new QLabel("End Page"));
}

int EndPage::nextId() const { return -1; }

main.cpp

#include "wizard.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Wizard w;
    w.show();
    return a.exec();
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值