首页 > 代码库 > Qt编程16:Qt向导简单使用(QWizard及QWizardPage)

Qt编程16:Qt向导简单使用(QWizard及QWizardPage)

关于Qt向导的使用,在Qt4.8.1以及4Q5.2.1中存在一个简单的例子,实现方式与我的方式有所不同。

在原有的例子中,使用了三个方法,分别定义了三个向导页;然后在main函数中将三个向导页通过addPage的方法加入向导。

如此比较简单实现,但是我觉得不够灵活。最后是将三个向导页分别继承QWizardPage,因为会涉及到每个向导页的一些操作。在这里也是基础。

 

mywizard.h

#ifndef MYWIZARD_H
#define MYWIZARD_H

#include <QWizard>
#include "firstwizard.h"
#include "secondwizard.h"
#include "thirdwizard.h"


class MyWizard : public QWizard
{
    Q_OBJECT
public:
    explicit MyWizard(QWidget *parent = 0);

signals:

private:
    FirstWizard     *m_Fw;
    SecondWizard    *m_Sw;
    ThirdWizard     *m_Tw;

public:
    enum
    {
        Page_First,
        Page_Second,
        Page_Third
    };

};

#endif // MYWIZARD_H


mywizard.cpp

#include "mywizard.h"
#include <QTextCodec>
#include <QIcon>

MyWizard::MyWizard(QWidget *parent) :
    QWizard(parent),
    m_Fw(new FirstWizard),
    m_Sw(new SecondWizard),
    m_Tw(new ThirdWizard)
{
    //将向导页加入向导
    setPage(MyWizard::Page_First,m_Fw);
    setPage(MyWizard::Page_Second,m_Sw);
    setPage(MyWizard::Page_Third,m_Tw);
    //去掉帮助按钮
    this->setWindowFlags(windowFlags()&~Qt::WindowContextHelpButtonHint);
    //设置导航样式
    setWizardStyle( QWizard::ModernStyle );
    setWindowTitle( "Qt 向导页面");

    //去掉向导页面按钮
    setOption( QWizard::NoBackButtonOnStartPage );
    setOption( QWizard::NoBackButtonOnLastPage );
    setOption( QWizard::NoCancelButton );

    //---------------------------------------------------
    //在子页面中设置title一下几行才会生效
    QPixmap pix(QSize(500, 78));
    pix.fill(QColor(173,173,173));
    setPixmap(QWizard::BannerPixmap,pix);

    QIcon icon(":/new/images/infor.png");
    setPixmap( QWizard::LogoPixmap,icon.pixmap(64));

    //---------------------------------------------------
    //设置页面主标题的显示格式
    setTitleFormat(Qt::RichText);
    //设置子标题显示格式
    setSubTitleFormat(Qt::RichText);

    //设置两个按钮
    setButtonText(QWizard::NextButton, "下一步>");
    setButtonText(QWizard::BackButton, "<上一步");

}

firstwizard.h

#ifndef FIRSTWIZARD_H
#define FIRSTWIZARD_H

#include <QWizardPage>

namespace Ui {
class FirstWizard;
}

class FirstWizard : public QWizardPage
{
    Q_OBJECT

public:
    explicit FirstWizard(QWizardPage *parent = 0);
    ~FirstWizard();

private:
    Ui::FirstWizard *ui;
};

QString titleTemplate();
QString subTitleTemplate();

#endif // FIRSTWIZARD_H

firstwizard.cpp

#include "firstwizard.h"
#include "ui_firstwizard.h"
#include <QLabel>


FirstWizard::FirstWizard(QWizardPage *parent) :
    QWizardPage(parent),
    ui(new Ui::FirstWizard)
{
    ui->setupUi(this);
    setTitle(titleTemplate().arg("Qt软件向导使用"));
    setSubTitle(subTitleTemplate().arg("博客地址:http://blog.csdn.net/jiezhj"));
}

FirstWizard::~FirstWizard()
{
    delete ui;
}


QString titleTemplate()
{
  return QString::fromLatin1("<font color=\"white\" size=\"5\">")+ QString::fromLatin1("%1</font>");
}

QString subTitleTemplate()
{
    return QString::fromLatin1("<font color=\"white\" size=\"3\">")+ QString::fromLatin1("%1</font>");
}

其余两个页面与第一个页面类似