qtqt5qwidgetqwizardqwizardpage

How to make widget that would be displayed on all QWizardPages without dublicate it in every Page?


Qt5. I have the main class class StartupWizard : public QWizard

and QWizardPages like these: class IntroPage : public QWizardPage

It begins with set pages:

   setPage(Page_Intro, m_introPage);
   setPage(Page_UserData, m_fcsPage);

   for (int i = 0; i < m_rolesPages.size(); i++) {
      setPage(i + 2, m_rolesPages.at(i));
   }

   setStartId(Page_Intro);
   setOption(QWizard::HaveHelpButton);

and method int nextId() const override; is overrided in every page

All works correct, but when I want to insert one general widget, button, for example, that would be displayed in every QWizardPage i get: enter image description here

Initialization of this button is:

   QVBoxLayout m_lay;
   m_lay.addWidget(m_button);
   setLayout(&m_lay);

It could be no button, I show it as an example... setSideWidget is shown on the left side of the wizard, but I heed on top.So what I want to get:

enter image description here

The question is, how I can make a widget that would be displayed on all pages? In QWidget i use layouts and insertWidget, but I don't see it here...

Of course, I can send widget pointers to all pages, but have I another way to fix it?


Solution

  • A possible solution is to implement a custom QWizardPage that places a QVBoxLayout (and the pages that you want to display the same widget must inherit from that custom page). And every time the page is changed the widget is inserted in the QVBoxLayout.

    #include <QApplication>
    #include <QLabel>
    #include <QLineEdit>
    #include <QPointer>
    #include <QPushButton>
    #include <QVBoxLayout>
    #include <QWizardPage>
    
    #include <QDebug>
    
    class BasePage: public QWizardPage{
        Q_OBJECT
    public:
        BasePage(QWidget *parent=nullptr): QWizardPage(parent), m_layout(new QVBoxLayout){
            setLayout(m_layout);
        }
        void setWidget(QWidget * widget){
            if(widget)
                m_layout->insertWidget(0, widget);
        }
    
        QVBoxLayout *verticalLayout() const{
            return m_layout;
        }
    private:
        QVBoxLayout *m_layout;
    };
    
    class Wizard: public QWizard{
    public:
        Wizard(QWidget *parent=nullptr): QWizard(parent){
            connect(this, &QWizard::currentIdChanged, this, &Wizard::change);
        }
        QWidget *widget() const{
            return m_widget;
        }
        void setWidget(QWidget *widget){
            m_widget = widget;
            change();
        }
    private:
        void change(){
            if(BasePage *next_page = qobject_cast<BasePage*>(currentPage())){
                next_page->setWidget(m_widget);
            }
        }
        QPointer<QWidget> m_widget;
    };
    
    
    class FirstPage: public BasePage{
    public:
        FirstPage(QWidget *parent=nullptr): BasePage(parent){
            verticalLayout()->addWidget(new QLabel("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse id justo vel nibh egestas interdum quis vel massa. Curabitur fringilla dui nibh, in cursus libero luctus et. Nullam hendrerit nunc erat, id tempor augue laoreet vitae."));
            verticalLayout()->addWidget(new QLineEdit);
            verticalLayout()->addWidget(new QLabel("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse id justo vel nibh egestas interdum quis vel massa. Curabitur fringilla dui nibh, in cursus libero luctus et. Nullam hendrerit nunc erat, id tempor augue laoreet vitae."));
        }
    };
    
    class SecondPage: public BasePage{
    public:
        SecondPage(QWidget *parent=nullptr): BasePage(parent){
            verticalLayout()->addWidget(new QLineEdit);
            verticalLayout()->addWidget(new QLabel("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse id justo vel nibh egestas interdum quis vel massa. Curabitur fringilla dui nibh, in cursus libero luctus et. Nullam hendrerit nunc erat, id tempor augue laoreet vitae."));
            verticalLayout()->addWidget(new QLineEdit);
        }
    };
    
    #include "main.moc"
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Wizard wizard;
        wizard.setWidget(new QPushButton("Qt is awesome!!!"));
        wizard.addPage(new FirstPage);
        wizard.addPage(new SecondPage);
        wizard.resize(640, 480);
        wizard.show();
        return a.exec();
    }