QT 可扩展对话框实例

extensiondlg.h

#ifndef EXTENSIONDLG_H
#define EXTENSIONDLG_H

#include <QDialog>

class ExtensionDlg : public QDialog
{
    
    
    Q_OBJECT

public:
    ExtensionDlg(QWidget *parent = nullptr);
    ~ExtensionDlg();
private slots:
    void showDetailInfo();
private:
    void createBaseInfo();
    void createDetailInfo();
    QWidget *baseWidget;
    QWidget *detailWidget;
};
#endif // EXTENSIONDLG_H

extensiondlg.cpp

#include "extensiondlg.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QComboBox>
#include <QPushButton>
#include <QDialogButtonBox>
#include <QHBoxLayout>

ExtensionDlg::ExtensionDlg(QWidget *parent)
    : QDialog(parent)
{
    
    
    setWindowTitle("Extension Dialog");
    createBaseInfo();
    createDetailInfo();

    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(baseWidget);
    layout->addWidget(detailWidget);
    layout->setSizeConstraint(QLayout::SetFixedSize);
    layout->setSpacing(10);

}

ExtensionDlg::~ExtensionDlg()
{
    
    
}

void ExtensionDlg::createBaseInfo()
{
    
    
    baseWidget = new QWidget();

    QLabel *nameLabel = new QLabel("姓名: ");
    QLineEdit *nameLineEdit = new QLineEdit();

    QLabel *sexLabel = new QLabel("性别: ");
    QComboBox *sexComboBox = new QComboBox();
    sexComboBox->insertItem(0, "女");
    sexComboBox->insertItem(1, "男");

    QGridLayout *LeftLayout = new QGridLayout();
    LeftLayout->addWidget(nameLabel, 0, 0);
    LeftLayout->addWidget(nameLineEdit, 0, 1);
    LeftLayout->addWidget(sexLabel);
    LeftLayout->addWidget(sexComboBox);

    QPushButton *OKBtn = new QPushButton("确认");
    QPushButton *DetailBtn = new QPushButton("详细");
    QDialogButtonBox *btnBox = new QDialogButtonBox(Qt::Vertical);
    btnBox->addButton(OKBtn, QDialogButtonBox::ActionRole);
    btnBox->addButton(DetailBtn, QDialogButtonBox::ActionRole);

    QHBoxLayout *mainLayout = new QHBoxLayout(baseWidget);
    mainLayout->addLayout(LeftLayout);
    mainLayout->addWidget(btnBox);
    connect(DetailBtn, SIGNAL(clicked()), this, SLOT(showDetailInfo()));

}

void ExtensionDlg::createDetailInfo()
{
    
    
    detailWidget = new QWidget();
    QLabel *ageLabel = new QLabel("年龄: ");
    QLineEdit *ageLineEdit = new QLineEdit();
    ageLineEdit->setText("30");

    QLabel *departmentLabel = new QLabel("部门: ");
    QComboBox *departmentCombox = new QComboBox();
    departmentCombox->addItem("研发部");
    departmentCombox->addItem("销售部");
    departmentCombox->addItem("资材部");
    departmentCombox->addItem("财务部");

    QLabel *emailLabel = new QLabel("Email: ");
    QLineEdit *emailLineEdit = new QLineEdit();

    QGridLayout *mainLayout = new QGridLayout(detailWidget);
    mainLayout->addWidget(ageLabel, 0, 0);
    mainLayout->addWidget(ageLineEdit, 0, 1);
    mainLayout->addWidget(departmentLabel, 1, 0);
    mainLayout->addWidget(departmentCombox, 1, 1);
    mainLayout->addWidget(emailLabel, 2, 0);
    mainLayout->addWidget(emailLineEdit, 2, 1);

    detailWidget->hide();
}

void ExtensionDlg::showDetailInfo()
{
    
    
    if (detailWidget->isHidden()) {
    
    
        detailWidget->show();
    } else {
    
    
        detailWidget->hide();
    }
}

main.cpp

#include "extensiondlg.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    
    
    QApplication a(argc, argv);
    ExtensionDlg w;
    w.show();
    return a.exec();
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/u013420428/article/details/109723062