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 

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中

你可能感兴趣的:(qt)