qt5 扩展式弹窗的使用例子

使用版本: qt creator 4.0.3 和 qt 5.6.2

简单例子(不使用ui文件, 用代码生成ui界面)

test.pro

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = untitled1
TEMPLATE = app


SOURCES += main.cpp\
        test.cpp

HEADERS  += test.h

test.h

#ifndef TEST_H
#define TEST_H

#include <QDialog>

class QDialogButtonBox;
class QLabel;
class QLineEdit;
class QPushButton;

class test : public QDialog
{
    Q_OBJECT

public:
    test(QWidget *parent = 0);

private slots:
    void write();

private:
    QLabel *id;
    QLabel *password;
    QLabel *email;
    QLabel *telephone;
    QLineEdit *id_lineEdit;
    QLineEdit *ps_lineEdit;
    QLineEdit *em_lineEdit;
    QLineEdit *tel_lineEdit;

    QDialogButtonBox *buttonBox;
    QPushButton *loginButton;
    QPushButton *moreButton;
    QPushButton *registerButton;
    QWidget *extension;

};
#endif // TEST_H

test.cpp

#include <QtWidgets>

#include "test.h"

test::test(QWidget *parent)
    : QDialog(parent)
{
    id = new QLabel(tr("账号:"));
    id_lineEdit = new QLineEdit;
    id->setBuddy(id_lineEdit);

    password = new QLabel(tr("密码:"));
    ps_lineEdit = new QLineEdit;
    password->setBuddy(ps_lineEdit);

    email = new QLabel(tr("邮箱:"));
    em_lineEdit = new QLineEdit;
    email->setBuddy(em_lineEdit);

    telephone = new QLabel(tr("电话:"));
    tel_lineEdit = new QLineEdit;
    telephone->setBuddy(tel_lineEdit);

    loginButton = new QPushButton(tr("登录"));
    loginButton->setDefault(true);

    moreButton = new QPushButton(tr("还没注册?"));
    moreButton->setCheckable(true);
    moreButton->setAutoDefault(false);

    registerButton = new QPushButton(tr("注册"));

    extension = new QWidget;

    buttonBox = new QDialogButtonBox(Qt::Vertical);
    buttonBox->addButton(loginButton, QDialogButtonBox::ActionRole);
    buttonBox->addButton(moreButton, QDialogButtonBox::ActionRole);

    connect(moreButton, &QAbstractButton::toggled, extension, &QWidget::setVisible);

    connect(registerButton, SIGNAL(clicked()), this, SLOT(write()));

    connect(loginButton, SIGNAL(clicked()), this, SLOT(write()));

    QHBoxLayout *emLayout = new QHBoxLayout;
    emLayout->addWidget(email);
    emLayout->addWidget(em_lineEdit);

    QHBoxLayout *telLayout = new QHBoxLayout;
    telLayout->addWidget(telephone);
    telLayout->addWidget(tel_lineEdit);

    QVBoxLayout *extensionLayout = new QVBoxLayout;
    extensionLayout->setMargin(0);
    extensionLayout->addLayout(emLayout);
    extensionLayout->addLayout(telLayout);
    extensionLayout->addWidget(registerButton);
    extension->setLayout(extensionLayout);

    QHBoxLayout *idLayout = new QHBoxLayout;
    idLayout->addWidget(id);
    idLayout->addWidget(id_lineEdit);

    QHBoxLayout *psLayout = new QHBoxLayout;
    psLayout->addWidget(password);
    psLayout->addWidget(ps_lineEdit);

    QGridLayout *mainLayout = new QGridLayout;
    mainLayout->setSizeConstraint(QLayout::SetFixedSize);
    mainLayout->addLayout(idLayout, 0, 0);
    mainLayout->addLayout(psLayout, 1, 0);
    mainLayout->addWidget(buttonBox, 0, 2, 2, 1);
    mainLayout->addWidget(extension, 3, 0, 2, 2);
     mainLayout->setRowStretch(2, 1);

    setLayout(mainLayout);

    setWindowTitle(tr("test"));
    extension->hide();
}

void test::write()
{
    QFile read_file("./file.txt");
    read_file.open(QIODevice::ReadOnly);
    QTextStream in(&read_file);
    QString line = in.readAll();
    read_file.close();

    QFile write_file("./file.txt");
    write_file.open(QIODevice::WriteOnly);
    QTextStream out(&write_file);
    out << line << "\r\n" << id_lineEdit->text() << " " << ps_lineEdit->text() << " " <<
          em_lineEdit->text() << " " << tel_lineEdit->text();
   write_file.close();
}

可以直接运行, 功能为: 点击还没注册弹出扩展框, 点击登录或者注册将填写的信息写入到txt中

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值