用Qt制作一个简单的属性页对话框

1、功能效果

在这里插入图片描述
点击“设置消息”隐藏主界面,弹出如下对话框界面:
在这里插入图片描述
点击“下一步”弹出如下:
在这里插入图片描述
点击“完成”弹出主界面如下:
在这里插入图片描述

2、代码结构

在这里插入图片描述
其中子对话框的添加主要通过:“文件—新建–Qt设计师界面类”来添加
在这里插入图片描述

3、源码

property1dlg.h

#ifndef PROPERTY1DLG_H
#define PROPERTY1DLG_H

#include <QWidget>
#include <QButtonGroup>

namespace Ui {
    
    
class Property1Dlg;
}

class Property1Dlg : public QWidget
{
    
    
    Q_OBJECT

public:
    explicit Property1Dlg(QWidget *parent = nullptr);
    ~Property1Dlg();

signals:
    void BackToMain_Signal();
    void GoToNext_Signal(QString strName,int nSex,QString strAge,QString strSchool);

private slots:
    void on_CancelButton_clicked();
    void on_DownButton_clicked();
    void slots_Sex();

private:
    Ui::Property1Dlg *ui;
    QButtonGroup *groupButton;
    int m_nSex;
};

#endif // PROPERTY1DLG_H

property1dlg.cpp

#include "property1dlg.h"
#include "ui_property1dlg.h"

Property1Dlg::Property1Dlg(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Property1Dlg)
{
    
    
    ui->setupUi(this);
    groupButton=new QButtonGroup(this);
    groupButton->addButton(ui->BoyRadioBtn,0);
    groupButton->addButton(ui->GirlRadioBtn,1);
    ui->BoyRadioBtn->setChecked(true); //默认选中
    m_nSex = 0;
    connect(ui->BoyRadioBtn,SIGNAL(clicked(bool)),this,SLOT(slots_Sex()));
    connect(ui->GirlRadioBtn,SIGNAL(clicked(bool)),this,SLOT(slots_Sex()));
}

Property1Dlg::~Property1Dlg()
{
    
    
    delete ui;
}

void Property1Dlg::on_CancelButton_clicked()
{
    
    
    close();
    emit BackToMain_Signal();
}

void Property1Dlg::on_DownButton_clicked()
{
    
    
    QString strName = ui->NameLineEdit->text();
    int nSex = m_nSex;
    QString strAge = ui->AgeLineEdit->text();
    QString strSchool = ui->SchoolComboBox->currentText();
    emit GoToNext_Signal(strName,nSex,strAge,strSchool);

    hide();
}

void Property1Dlg::slots_Sex()
{
    
    
    switch(groupButton->checkedId())
    {
    
    
    case 0:
        m_nSex = 0;
        break;
    case 1:
        m_nSex = 1;
        break;
    }
}

property2dlg.h

#ifndef PROPERTY2DLG_H
#define PROPERTY2DLG_H

#include <QWidget>

namespace Ui {
    
    
class property2dlg;
}

class property2dlg : public QWidget
{
    
    
    Q_OBJECT

public:
    explicit property2dlg(QWidget *parent = nullptr);
    ~property2dlg();

signals:
    void UpToPage1_Signal();
    void Finish_Signal(QString strJob,QString strMoney,QString strCity,QList<QString> aryHobey);

private slots:
    void on_FinishButton_clicked();
    void on_UpButton_clicked();

    void on_BasCheckBox_stateChanged(int arg1);

    void on_FootCheckBox_stateChanged(int arg1);

    void on_YMCheckBox_stateChanged(int arg1);

private:
    Ui::property2dlg *ui;
    QList<QString> m_aryHobey;
};

#endif // PROPERTY2DLG_H

property2dlg.cpp

#include "property2dlg.h"
#include "ui_property2dlg.h"
#pragma execution_character_set("utf-8")

property2dlg::property2dlg(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::property2dlg)
{
    
    
    ui->setupUi(this);
}

property2dlg::~property2dlg()
{
    
    
    delete ui;
}

void property2dlg::on_FinishButton_clicked()
{
    
    
    QString strJob = ui->WorkComboBox->currentText();
    QString strMoney = ui->MoneyComboBox->currentText();
    QString strCity = ui->CityLineEdit->text();
    QList<QString> aryHobey = m_aryHobey;

    emit Finish_Signal(strJob,strMoney,strCity,aryHobey);

    close();
}

void property2dlg::on_UpButton_clicked()
{
    
    
    hide();
    emit UpToPage1_Signal();
}

void property2dlg::on_BasCheckBox_stateChanged(int)
{
    
    
    QString strName = "篮球";
    int nIndex = m_aryHobey.indexOf(strName);

    if(ui->BasCheckBox->checkState() == Qt::Checked)
    {
    
    
        if(nIndex == -1)
        {
    
    
            m_aryHobey.append(strName);
        }
    }
    else
    {
    
    
        if(nIndex > -1)
        {
    
    
            m_aryHobey.removeAt(nIndex);
        }
    }
}

void property2dlg::on_FootCheckBox_stateChanged(int)
{
    
    
    QString strName = "足球";
    int nIndex = m_aryHobey.indexOf(strName);

    if(ui->FootCheckBox->checkState() == Qt::Checked)
    {
    
    
        if(nIndex == -1)
        {
    
    
            m_aryHobey.append(strName);
        }
    }
    else
    {
    
    
        if(nIndex > -1)
        {
    
    
            m_aryHobey.removeAt(nIndex);
        }
    }
}

void property2dlg::on_YMCheckBox_stateChanged(int)
{
    
    
    QString strName = "羽毛球";
    int nIndex = m_aryHobey.indexOf(strName);

    if(ui->YMCheckBox->checkState() == Qt::Checked && nIndex == -1)
    {
    
    
         m_aryHobey.append(strName);
    }
    else if(ui->YMCheckBox->checkState() == Qt::Unchecked && nIndex > -1)
    {
    
    
        m_aryHobey.removeAt(nIndex);
    }
}

propertywidget.h

#ifndef PROPERTYWIDGET_H
#define PROPERTYWIDGET_H

#include <QWidget>
#include "property1dlg.h"
#include "property2dlg.h"

QT_BEGIN_NAMESPACE
namespace Ui {
    
     class PropertyWidget; }
QT_END_NAMESPACE

class PropertyWidget : public QWidget
{
    
    
    Q_OBJECT

public:
    PropertyWidget(QWidget *parent = nullptr);
    ~PropertyWidget();

public slots:
    void BackToMainSlot();
    void GoToNextSlot(QString strName,int nSex,QString strAge,QString strSchool);
    void UpToPage();
    void FinishSet(QString strJob,QString strMoney,QString strCity,QList<QString> aryHobey);

private slots:
    void on_CancelButton_clicked();
    void on_InputButton_clicked();

public:
    QString m_strName;
    int m_nSex;
    QString m_strAge;
    QString m_strSchool;

    QString m_strJob;
    QString m_strMoney;
    QString m_strCity;
    QList<QString> m_aryHobey;

private:
    Ui::PropertyWidget *ui;
    Property1Dlg* pPage1Dlg;
    property2dlg* pPage2Dlg;
};
#endif // PROPERTYWIDGET_H

propertywidget.cpp

#include "propertywidget.h"
#include "ui_propertywidget.h"
#pragma execution_character_set("utf-8")

PropertyWidget::PropertyWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::PropertyWidget)
{
    
    
    ui->setupUi(this);

    pPage1Dlg = new Property1Dlg();
    pPage2Dlg = new property2dlg();

    connect(pPage1Dlg, &Property1Dlg::BackToMain_Signal, this, &PropertyWidget::BackToMainSlot);
    connect(pPage1Dlg, &Property1Dlg::GoToNext_Signal, this, &PropertyWidget::GoToNextSlot);
    connect(pPage2Dlg, &property2dlg::UpToPage1_Signal, this, &PropertyWidget::UpToPage);
    connect(pPage2Dlg, &property2dlg::Finish_Signal, this, &PropertyWidget::FinishSet);
}

PropertyWidget::~PropertyWidget()
{
    
    
    delete ui;
}

void PropertyWidget::BackToMainSlot()
{
    
    
    this->show();
}

void PropertyWidget::GoToNextSlot(QString strName, int nSex, QString strAge, QString strSchool)
{
    
    
    m_strName = strName;
    m_nSex = nSex;
    m_strAge = strAge;
    m_strSchool = strSchool;

    pPage2Dlg->show();
}

void PropertyWidget::UpToPage()
{
    
    
    pPage1Dlg->show();
}

void PropertyWidget::FinishSet(QString strJob,QString strMoney,QString strCity,QList<QString> aryHobey)
{
    
    
    m_strJob = "工作: " + strJob;
    m_strMoney = "收入: " + strMoney;
    m_strCity = "所在城市: " + strCity;
    m_aryHobey = aryHobey;
    QString strHobey = "爱好: ";
    for(int i = 0; i < m_aryHobey.length(); ++i)
    {
    
    
        if(i != m_aryHobey.length()-1)
        {
    
    
            QString sTemp = m_aryHobey.at(i);
            sTemp += ";";
            strHobey += sTemp;
        }
        else
        {
    
    
            strHobey += m_aryHobey.at(i);
        }
    }

    pPage1Dlg->close();
    this->show();

    ui->listWidget->clear();

    QString strName = "姓名: " + m_strName;
    ui->listWidget->addItem(strName);

    QString strSex = "性别: ";
    if(m_nSex == 0)
    {
    
    
        strSex += "男";
    }
    else
    {
    
    
        strSex += "女";
    }
    ui->listWidget->addItem(strSex);

    QString strAge = "年龄: " + m_strAge;
    ui->listWidget->addItem(strAge);

    QString strSchool = "学历: " + m_strSchool;
    ui->listWidget->addItem(strSchool);

    ui->listWidget->addItem(m_strJob);
    ui->listWidget->addItem(m_strMoney);
    ui->listWidget->addItem(m_strCity);
    ui->listWidget->addItem(strHobey);
}


void PropertyWidget::on_CancelButton_clicked()
{
    
    
    close();
}

void PropertyWidget::on_InputButton_clicked()
{
    
    
    this->hide();
    pPage1Dlg->show();
}

猜你喜欢

转载自blog.csdn.net/m0_37251750/article/details/119211968