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