0

Qt5。我有主课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显示在向导的左侧,但我注意顶部。所以我想得到什么:

在此处输入图像描述

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

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

4

1 回答 1

1

一种可能的解决方案是实现一个放置 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();
}
于 2021-10-20T08:29:00.420 回答