Search code examples
c++qtqwizard

QWizard - back button missing


I have a QWizard, which branches in a following way:

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

The problem is, the the end page only shows "back" button, when it's reached via StartPage -> page4 -> EndPage. Otherwise it only shows "finish" and "cancel". Does anyone have similar experiences, what could cause this? Here's how it's set up:

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

For the pages, nextId() is reimplemented to return correct id. I'm confused why it works differently via different paths.


Solution

  • Nobody can know what is wrong because you do not show your implementation so I will just provide a code that implements the scheme that shows:

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

    The complete example can be found here.