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

运行图

初始化隐藏

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

相关推荐
Hello-Mr.Wang4 分钟前
vue3中开发引导页的方法
开发语言·前端·javascript
救救孩子把7 分钟前
Java基础之IO流
java·开发语言
WG_178 分钟前
C++多态
开发语言·c++·面试
小菜yh9 分钟前
关于Redis
java·数据库·spring boot·redis·spring·缓存
宇卿.15 分钟前
Java键盘输入语句
java·开发语言
Amo Xiang25 分钟前
2024 Python3.10 系统入门+进阶(十五):文件及目录操作
开发语言·python
Microsoft Word29 分钟前
数据库系统原理(第一章 数据库概述)
数据库·oracle
friklogff38 分钟前
【C#生态园】提升C#开发效率:深入了解自然语言处理库与工具
开发语言·c#·区块链
华为云开源39 分钟前
openGemini 社区人才培养计划:助力成长,培养新一代云原生数据库人才
数据库·云原生·开源
重生之我在20年代敲代码2 小时前
strncpy函数的使用和模拟实现
c语言·开发语言·c++·经验分享·笔记