qt-16可扩展对话框--隐藏和展现

可扩展对话框

知识点

MainLayout->setSizeConstraint(QLayout::SetFixedSize);//固定窗口大小

extension.h

cpp 复制代码
#ifndef EXTENSION_H
#define EXTENSION_H

#include <QDialog>

class Extension : public QDialog
{
    Q_OBJECT

public:
    Extension(QWidget *parent = nullptr);
    ~Extension();
private slots:
    void ShowDetailInfo();
private:
    void CreateBaseInfo();
    void CreateDetailInfo();
    QWidget* BaseWidget;
    QWidget* DetailWidget;
};
#endif // EXTENSION_H

extension.cpp

cpp 复制代码
#include "extension.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QComboBox>
#include <QPushButton>
#include <QDialogButtonBox>
#include <QHBoxLayout>

Extension::Extension(QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle(tr("Extension Dialog"));
    CreateBaseInfo();
    CreateDetailInfo();
    QVBoxLayout* MainLayout = new QVBoxLayout(this);
    MainLayout->addWidget(BaseWidget);
    MainLayout->addWidget(DetailWidget);
    //MainLayout->setSizeConstraint(QLayout::SetFixedSize);//固定窗口大小
    MainLayout->setSpacing(10);
}

Extension::~Extension() {}

void Extension::ShowDetailInfo()
{
    if(DetailWidget->isHidden())
    {
        DetailWidget->show();
        return;
    }

    DetailWidget->hide();

}

void Extension::CreateBaseInfo()
{
    BaseWidget = new QWidget;
    QLabel* NameLabel = new QLabel(tr("姓名:"));
    QLineEdit* NameLineEdit = new QLineEdit;
    QLabel* SexLabel = new QLabel("性别:");
    QComboBox* SexComboBox = new QComboBox;
    SexComboBox->insertItem(0,tr("男"));
    SexComboBox->insertItem(1,tr("女"));
    QGridLayout* LeftLayout = new QGridLayout;
    LeftLayout->addWidget(NameLabel,0,0);
    LeftLayout->addWidget(NameLineEdit,0,1);
    LeftLayout->addWidget(SexLabel,1,0);
    LeftLayout->addWidget(SexComboBox,1,1);

    QPushButton* OkBtn = new QPushButton(tr("确定"));
    QPushButton* DetailBtn = new QPushButton(tr("详细"));
    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 Extension::CreateDetailInfo()
{
    DetailWidget = new QWidget;
    QLabel* AgeLabel = new QLabel(tr("年龄"));
    QLineEdit* AgeLineEdit = new QLineEdit(tr("30"));
    QLabel* DepartmentLabel = new QLabel(tr("部门:"));
    QComboBox* DepartmentComboBox = new QComboBox;
    DepartmentComboBox->addItem(tr("部门1"));
    DepartmentComboBox->addItem(tr("部门2"));
    DepartmentComboBox->addItem(tr("部门3"));
    DepartmentComboBox->addItem(tr("部门4"));
    QLabel* EmailLabel = new QLabel(tr("Email:"));
    QLineEdit* EmailEdit = new QLineEdit;

    //布局
    QGridLayout* MainLayout = new QGridLayout(DetailWidget);
    MainLayout->addWidget(AgeLabel,0,0);
    MainLayout->addWidget(AgeLineEdit,0,1);
    MainLayout->addWidget(DepartmentLabel,1,0);
    MainLayout->addWidget(DepartmentComboBox,1,1);
    MainLayout->addWidget(EmailLabel,2,0);
    MainLayout->addWidget(EmailEdit,2,1);

    DetailWidget->hide();

}

main.cpp

cpp 复制代码
#include "extension.h"

#include <QApplication>

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

运行图

初始化隐藏

展现--点击--详细按钮

相关推荐
看到我,请让我去学习33 分钟前
Qt— 布局综合项目(Splitter,Stacked,Dock)
开发语言·qt
创想未来CTF33 分钟前
Qt同步处理业务并禁用按钮
qt
代码的余温36 分钟前
SQL性能优化全攻略
数据库·mysql·性能优化
GUET_一路向前1 小时前
【C语言防御性编程】if条件常量在前,变量在后
c语言·开发语言·if-else·防御性编程
曳渔1 小时前
UDP/TCP套接字编程简单实战指南
java·开发语言·网络·网络协议·tcp/ip·udp
三千道应用题1 小时前
WPF&C#超市管理系统(6)订单详情、顾客注册、商品销售排行查询和库存提示、LiveChat报表
开发语言·c#·wpf
hqxstudying1 小时前
JAVA项目中邮件发送功能
java·开发语言·python·邮件
咪咪渝粮1 小时前
JavaScript 中constructor 属性的指向异常问题
开发语言·javascript
最初的↘那颗心1 小时前
Java HashMap深度解析:原理、实现与最佳实践
java·开发语言·面试·hashmap·八股文
后台开发者Ethan2 小时前
Python需要了解的一些知识
开发语言·人工智能·python