如何制作将显示在所有 QWizardPage 上的小部件而不在每个页面中重复它?

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

Qt5。我有主要的 class class StartupWizard : public QWizard

QWizardPages像这样:class IntroPage : public QWizardPage

它从设置的页面开始:

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

并且方法 int nextId() const override; 在每个页面中都被覆盖

一切正常,但是当我想插入一个通用小部件时,例如按钮,它会显示在每个 QWizardPage 中,我得到:

这个按钮的初始化是:

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

可能没有按钮,我举个例子... setSideWidget显示在向导的左侧,但我注意到top.So我想得到的是:

问题是,如何制作一个可以在所有页面上显示的小部件?在 QWidget 中,我使用 layoutsinsertWidget,但我在这里看不到它...

当然,我可以将小部件指针发送到所有页面,但是我有其他方法来修复它吗?

一个可能的解决方案是实现一个放置 QVBoxLayout 的自定义 QWizardPage(并且您要显示相同小部件的页面必须从该自定义页面继承)。每次更改页面时,都会将小部件插入 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();
}