Qt : 可扩展对话框的实现

这里是一个简单的可扩展对话框的小例子:窗口可扩展,但是各部件的位置没有变化.
这里写图片描述

#include "c.h"

c::c(QWidget *parent)
    : QWidget(parent)
{
//  ui.setupUi(this);

    QGridLayout *layout = new QGridLayout(this);

    m_nameLabel = new QLabel(QString::fromLocal8Bit("姓名:"));
    m_ageLabel = new QLabel(QString::fromLocal8Bit("年龄:"));
    m_addressLabel = new QLabel(QString::fromLocal8Bit("家庭地址:"));
    m_fatherLabel = new QLabel(QString::fromLocal8Bit("父亲:"));
    m_motherLabel = new QLabel(QString::fromLocal8Bit("母亲:"));
    m_nameLineEdit = new QLineEdit();
    m_ageLineEdit = new QLineEdit();
    m_addressLineEdit = new QLineEdit();
    m_fatherLineEdit = new QLineEdit();
    m_motherLineEdit = new QLineEdit();
    m_sureButton = new QPushButton(QString::fromLocal8Bit("确定"));
    m_detailButton = new QPushButton(QString::fromLocal8Bit("详细信息"));
    m_detailButton->setCheckable(true);

    layout->addWidget(m_nameLabel, 0, 0);
    layout->addWidget(m_ageLabel, 1, 0);
    layout->addWidget(m_addressLabel, 2, 0);
    layout->addWidget(m_fatherLabel, 3, 0);
    layout->addWidget(m_motherLabel, 4, 0);
    layout->addWidget(m_nameLineEdit, 0, 1);
    layout->addWidget(m_ageLineEdit, 1, 1);
    layout->addWidget(m_addressLineEdit, 2, 1);
    layout->addWidget(m_fatherLineEdit, 3, 1);
    layout->addWidget(m_motherLineEdit, 4, 1);
    layout->addWidget(m_sureButton, 1, 2);
    layout->addWidget(m_detailButton, 2, 2);

    m_fatherLineEdit->setVisible(false);
    m_motherLineEdit->setVisible(false);
    m_fatherLabel->setVisible(false);
    m_motherLabel->setVisible(false);

    //设置布局限制,保持固定的尺寸.
    layout->setSizeConstraint(QLayout::SetFixedSize);

    connect(m_detailButton, SIGNAL(toggled(bool)),this, SLOT(showDetailContentsSlot(bool)));
}

c::~c()
{

}
void c::showDetailContentsSlot(bool status)
{
    if (status)
    {
        m_fatherLineEdit->setVisible(true);
        m_motherLineEdit->setVisible(true);
        m_fatherLabel->setVisible(true);
        m_motherLabel->setVisible(true);
    }
    else
    {
        m_fatherLineEdit->setVisible(false);
        m_motherLineEdit->setVisible(false);
        m_fatherLabel->setVisible(false);
        m_motherLabel->setVisible(false);
    }
}

上面布局的示意图如下:只不过设置控件在布局中的位置.
这里写图片描述

下面是一些成员变量的定义.

#ifndef C_H
#define C_H

#include <QtWidgets/QWidget>
#include "ui_c.h"
#include <QGridLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>

class c : public QWidget
{
    Q_OBJECT

public:
    c(QWidget *parent = 0);
    ~c();
private slots:
    void showDetailContentsSlot(bool);
private:
    Ui::cClass ui;
    QLabel *m_nameLabel;
    QLabel *m_ageLabel;
    QLabel *m_addressLabel;
    QLabel *m_fatherLabel;
    QLabel *m_motherLabel;
    QLineEdit *m_nameLineEdit;
    QLineEdit *m_ageLineEdit;
    QLineEdit *m_addressLineEdit;
    QLineEdit *m_fatherLineEdit;
    QLineEdit *m_motherLineEdit;
    QPushButton *m_sureButton;
    QPushButton *m_detailButton;
};

#endif // C_H
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值