QWizard-缺少后退按钮

时间:2018-12-05 10:44:29

标签: c++ qt qwizard

我有一个QWizard,它以下列方式分支:

           ----- Page1 -----
         /                   \
StartPage  - Page2 - Page3 -  EndPage
         \                   /
           ----- Page4 -----

问题是,当通过StartPage-> page4-> EndPage到达末页时,末页仅显示“返回”按钮。否则,它仅显示“完成”和“取消”。有谁有类似的经历,是什么原因造成的?设置方法如下:

QWizard wizard;
wizard.setPage(0, new StartPage(&wizard));
wizard.setPage(1, new Page1(&wizard));
wizard.setPage(2, new Page2(&wizard));
wizard.setPage(3, new Page3(&wizard));
wizard.setPage(4, new Page4(&wizard));
wizard.setPage(5, new EndPage(&wizard));

对于页面,将重新实现nextId()以返回正确的ID。我很困惑为什么它通过不同的路径会有所不同。

1 个答案:

答案 0 :(得分:0)

没人知道哪里出了问题,因为您没有显示实现,所以我只提供实现以下方案的代码:

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

完整示例见here